Question

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

Was it helpful?

Solution

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());
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top