chris1out
chris1out

Reputation: 176

ASP.NET core exception handling in view

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?

Upvotes: 4

Views: 826

Answers (1)

adem caglin
adem caglin

Reputation: 24063

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);
    }
}

Upvotes: 4

Related Questions