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:
- [MyExceptionFilter]
- public class HomeController : Controller
- {
- [OverrideExceptionFiltersAttribute]
- public ActionResult Index()
- {
- int a = 10;
- int b = 0;
- int c = a / b;
-
- return View();
- }
-
- public ActionResult About()
- {
- ViewBag.Message = "Your application description page.";
- return View();
- }
-
- public ActionResult Contact()
- {
- ViewBag.Message = "Your contact page.";
- return View();
- }
- }
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.