How to get ASP.NET MVC IAuthorizationFilter to respect IgnoreRoute's

748 Views Asked by At

I am using the popular Elmah.MVC error logging NuGet package. So I can navigate to /elmah to view my error logs. I have the following IgnoreRoute statements, so that MVC ignores the /elmah route:

routes.IgnoreRoute("elmah");
routes.IgnoreRoute("elmah/{*pathinfo}");

I have written the following authorization filter. How can I stop the filter below from executing when I navigate to /elmah. Surely there must be some way for it to respect the routes I tell it to ignore?

public class MyFilterAttribute : FilterAttribute, IAuthorizationFilter
{
    public void OnAuthorization(AuthorizationContext filterContext)
    {
        // I don't want /elmah routes to execute this.
    }
}
1

There are 1 best solutions below

0
On

The solution was to check the ControllerName that was executing and if it is Elmah, we return and ignore this request.

public class MyFilterAttribute : FilterAttribute, IAuthorizationFilter
{
    public void OnAuthorization(AuthorizationContext filterContext)
    {
        // I don't want /elmah routes to execute for this filter.
        string controllerName = filterContext.ActionDescriptor.ControllerDescriptor.ControllerName;
        if (string.Equals(controllerName, "Elmah", StringComparison.Ordinal))
        {
            return;
        }
    }
}