문제

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

도움이 되었습니까?

해결책

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());
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top