Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When will an MVC ExceptionFilter be executed vs the application-level error handler?

I have a custom exception FilterAttribute such as the following:

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = true)]
public sealed class ExceptionLoggingFilterAttribute : FilterAttribute, IExceptionFilter
{
    public void OnException(ExceptionContext filterContext)
    {
        if (filterContext == null)
        {
            throw new ArgumentNullException(nameof(filterContext));
        }

        if (filterContext.ExceptionHandled)
        {
            return;
        }

        // some exception logging code (not shown)

        filterContext.ExceptionHandled = true;
}

I have this registered globally in my FilterConfig.cs

public static class FilterConfig
{
    public static void RegisterGlobalFilters(GlobalFilterCollection filters)
    {
        filters?.Add(new ExceptionLoggingFilterAttribute());
    }
}

I also have an Application_Error method declared in my global.asax.cs

protected void Application_Error(object sender, EventArgs e)
    {
        var exception = Server.GetLastError();

        // some exception logging code (not shown)
    }
  1. When will the exception filter code be hit and when will it go straight to the global error handler in the Application_Error method? (I understand the ExceptionHandled concept and realise that by marking that as handled in my filter, it won't then cascade up to the global error handler).

An exception that I thought would hit the filter - an HttpException for 404, does not hit the filter but does get caught in the application error handler.

  1. I have seen some code samples where people use the HttpContext.Current in the global.asax.cs to do a Server.TransferRequest to a specific error view. Is this best practice? Would it be better to use the CustomErrors section in the system.web section of the web.config?
like image 262
obaylis Avatar asked May 22 '17 13:05

obaylis


People also ask

How does MVC handle application error in global ASAX?

Setting HandleError Attribute as a Global Filter Any unhandled exception that takes place within the boundary of the MVC application will now be handled by this global error handler. To test this global handler, comment out the [HandleError] attribute from the action or the controller and then run the application.

When exception filter is executed?

These implement IExceptionFilter and execute if there is an unhandled exception thrown during the execution of the ASP.NET MVC pipeline. Exception filters can be used for tasks such as logging or displaying an error page. The HandleErrorAttribute class is one example of an exception filter.

How do we use handle error in MVC?

HandleErrorAttribute. The HandleErrorAttribute is an attribute that can be used to handle exceptions thrown by an action method or a controller. You can use it to display a custom view on a specific exception occurred in an action method or in an entire controller.

Which filter will be executed at first using ASP.NET MVC?

as you can see from the below diagram, as soon as the controller starts execution through Action Invoker, Authentication and authorization filters are the very first filters to be triggered, followed by model binding which maps request and route data to action parameters.


1 Answers

An exception filter will be hit only for errors that occur during the execution of the ASP.NET MVC pipeline, e.g. during the execution of an Action method:

Exception filters. These implement IExceptionFilter and execute if there is an unhandled exception thrown during the execution of the ASP.NET MVC pipeline. Exception filters can be used for tasks such as logging or displaying an error page. The HandleErrorAttribute class is one example of an exception filter.

(from: https://msdn.microsoft.com/en-us/library/gg416513(VS.98).aspx)

In the case of a 404 error, an Action method could not be determined, so that the error is not handled in the filter.

All other errors will be handled in the Application_Error method.

As for the second part of your question, I'd recommend the following blog post that contains a good overview on how to set up custom error pages in a reliable way: http://benfoster.io/blog/aspnet-mvc-custom-error-pages

like image 137
Markus Avatar answered Oct 06 '22 00:10

Markus