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.
- [MyActionFilter]
- public class HomeController : Controller
- {
- public ActionResult Index()
- {
- return View();
- }
-
- [OverrideActionFilters]
- 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 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.