Search code examples
c#.netasp.net-mvcexceptionaction-filter

How to handle an exception occurring in a filter?


I have various filters before a URL hits an controller action.

Very few filters have a complex logic which might throw exceptions.

Now, how do I catch these exceptions ?

For eg. I have a filter which handles exception occuring in a controller method.

[ActionFilter1] is a filter which handles any exception occuring in a controller method.

public override void OnActionExecuting(ActionExecutingContext filterContext)
{

 //exception occuring here

}

One way is to do something like this:

public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
     try {
     //exception occuring here
     }
     catch {
     //log here
     }
    }

since there are n filters, I do not want to repeat this logic of adding try & catch to each and every filter.

In that case, how can I proceed with a single place error handling which can handle any exceptions occurring in all these filters ?


Solution

  • Read this article. You will find the answer http://www.codeproject.com/Articles/850062/Exception-handling-in-ASP-NET-MVC-methods-explaine