Override Exception Filter In MVC

In my previous blog, we discussed about how we can use the exception filters to handle the exceptions at a common location for all the methods of the controller. 
 
However, we may have a situation, where we do not need to log the exception for all the methods of the controller. For this, we can use the OverrideExceptionFilters attribute on the methods for which we do not need the action filter to be executed. Simply, apply this attribute on the controller method and the filter will not be executed for that method. See the code given below: 
  1. [MyExceptionFilter]  
  2. public class HomeController : Controller  
  3. {  
  4.     [OverrideExceptionFiltersAttribute]  
  5.     public ActionResult Index()  
  6.     {  
  7.         int a = 10;  
  8.         int b = 0;  
  9.         int c = a / b;  
  10.   
  11.         return View();  
  12.     }  
  13.   
  14.     public ActionResult About()  
  15.     {  
  16.         ViewBag.Message = "Your application description page.";  
  17.         return View();  
  18.     }  
  19.   
  20.     public ActionResult Contact()  
  21.     {  
  22.         ViewBag.Message = "Your contact page.";  
  23.         return View();  
  24.     }  
  25. }  
Now, run the Application, add debugger on the Index method of the controller and also the methods of the exception filter. The filter methods will not be executed for the Index method. Happy coding.
Ebook Download
View all
Learn
View all