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

Était-ce utile?

La 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());
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top