MVC 4 Global Exception Filter how to implement?

前端 未结 2 485
孤城傲影
孤城傲影 2020-12-30 04:54

How do I implement a global exception handler in MVC4 as it seems to be different from MVC3.

Not sure how to implement the following:

public class E         


        
2条回答
  •  鱼传尺愫
    2020-12-30 05:02

    The way I created an exception handler for MVC part of it, I created a class that implemented IExceptionFilter

    public class MVCExceptionFilter : IExceptionFilter
    {
        public void OnException(ExceptionContext filterContext)
        {
            Trace.TraceError(filterContext.Exception.ToString());
        }
    }
    

    You then register it in the Global.asax.cs inside protected void Application_Start()

    The method already contains the line

    FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
    

    So, you will need to add this line ABOVE it

    GlobalFilters.Filters.Add(new MVCExceptionFilter());
    

提交回复
热议问题