Override Action Filter In MVC

In my previous blog, we discussed how we can use the action filters to handle a scenario, where we need to perform an operation before and after the execution of any controller method. We created a filter and applied it on the controller for all the methods.
 
However, in real time, we may have a situation, where we do not need to use it for all the methods of the controller. For this, we can use the OverrideActionFilters 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. [MyActionFilter]  
  2. public class HomeController : Controller  
  3. {  
  4.     public ActionResult Index()  
  5.     {  
  6.         return View();  
  7.     }  
  8.   
  9.     [OverrideActionFilters]  
  10.     public ActionResult About()  
  11.     {  
  12.         ViewBag.Message = "Your application description page.";  
  13.         return View();  
  14.     }  
  15.   
  16.     public ActionResult Contact()  
  17.     {  
  18.         ViewBag.Message = "Your contact page.";  
  19.         return View();  
  20.     }  
  21. }  
Now, run the application and add debuggers on the Index, about methods of the controller, and also the methods of the action filter. The filter methods will be executed for the Index but not about the method.
 
Happy coding.  
Ebook Download
View all
Learn
View all