4

Why does an exception thrown in an asp.net core view not go through the global exception filter? How can I catch and log those exceptions?

1 Answer 1

4

Since exception filter is executed before view execution, you can not catch exception in the view with using exception filter. To catch this type of exceptions:

1- You can use UseExceptionHandler to globally handle all exception(This is not mvc specific solution).

2- Using ResultFilter to catch exception in view(this is aware of mvc context):

public class ExceptionResultFilter : ResultFilterAttribute
{
    public override void OnResultExecuted(ResultExecutedContext context)
    {
        if(context.Exception != null)
        {
            // log exception
        }
        base.OnResultExecuted(context);
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.