How to use Exception filters in MVC 5

前端 未结 4 808
没有蜡笔的小新
没有蜡笔的小新 2021-01-04 08:57

How i can implement Exception Filters in MVC5.

I want to throw the exception to NLog and redirect the page to a default error page which displays \"Something is gone

4条回答
  •  自闭症患者
    2021-01-04 09:14

    I believe it should be this code:

    public class MyExceptionFilter : IExceptionFilter
    {
        public void OnException(ExceptionContext filterContext)
        {
            filterContext.Result = new RedirectToRouteResult(new RouteValueDictionary(new { 
                action = "UserLogOut", 
                controller = "User", 
                area = "User" 
            }));
    
        }
    

    }

    You may add an additional "if (!filterContext.ExceptionHandled)" statement before logging the values inside the result to make sure that the exception's unhandled for the moment.

提交回复
热议问题