ASP.NET MVC Applications contains the action methods in the controllers. These actions are mapped through the routing when a user makes a browser request. In order to execute your logic before a action method is called or after an action method runs. ASP.NET MVC support this requirement through a concept called ActionFilters.
- Authentication and Authorization can be implemented using action filters.
- User actions can be logged.
- For setting up the Localization feature in the application.
- For Changing the Application behavior based on browser user agent.
Creating an ActionFilter in ASP.NET MVC Application
You can write a ActionFilter class by inherting from ActionFileAttribute class. You can override the method OnActionExecuting and write the logic that executes before Action Method.
- To Add a ActionFilter to the ASP.NET MVC Application right click the Controllers folder in the project and select the add new item option and add the class template write the following code.
2. Applying Filtering attribute to the action method in the controller class
You can also apply the ActionFilter to all action methods in the controller.