3

In my project I have different Areas. I have added one ExceptionFilterAttribute for handling errors(custom). I don't want to add this attribute to all controllers manually, i want to register this attribute to a specific Area, so ExceptionFilterAttribute will work will all controller in that area automatically

Any suggestions

Bibin
  • 215
  • 2
  • 4
  • 10

1 Answers1

3

There's no way to apply an action filter to an area. You could write a custom exception filter attribute and then apply it as a global action filter:

public class AdminAreaHandleErrorAttribute: HandleErrorAttribute
{
    public override void OnException(ExceptionContext filterContext)
    {
        var area = filterContext.RouteData.Values["area"] as string;
        if (string.Equals(area, "Admin", StringComparison.InvariantCultureIgnoreCase))
        {
            base.OnException(filterContext);
        }
    }
}

and then register as global filter:

public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
    filters.Add(new AdminAreaHandleError());
}
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928