Action filters are another kind of attribute you can apply to action methods inside of a controller. They can be used to perform logic or code before or after an action method runs.
There are different kinds of action filters built-in for your use such as HandleErrorAttribute and OutputCacheAttribute filters. You can even customize your own action filters.
For example, there's the built-in AuthorizeAttribute filter, which can be used to ensure a user is logged in before the action can run. If they are not, the user can be sent to the login screen and after logging in, the action will run. You can even limit the action to specific users like a user who has the role of admin.
Apply an Action Filter to a Single Action
To apply an action filter to an action, simply place the attribute before the method like you would with action selectors. For the code below, adding [Authorize(Role="Admin")] before the Search() method checks to see if a user is logged in and has the role of admin before the Search action can be executed.
public class CuisineController : Controller { [Authorize(Role="Admin")] public ActionResult Search(string name = "french") { var message = Server.HtmlEncode(name); return Content(message); } }
Note: You can add an action filter to a single action, or an entire controller, or even apply it to the global filters.
Apply an Action Filter to an Entire Controller
Add the action filter above the controller class definition to have it apply to all of the controller's actions. In the code below, the action filter [Authorize] requires users to be logged in to run the any action in this controller.
[Authorize] public class CuisineController : Controller { // Action methods ... }
Apply an Action Filter Globally
Global filters are registered during the application start up. To configure what filters are added during the application start event, we need to modify the RegisterGlobalFilters method located in the FilterConfig.cs file that resides in the App_Start folder.
These filters will be applied to every single request that your controllers process.
public class FilterConfig { public static void RegisterGlobalFilters(GlobalFilterCollection filters) { // HandleErrorAttribute() is a filter for handling errors // Used to display a friendly error page to users when // something goes wrong. filters.Add(new HandleErrorAttribute()); } }
How to Customize Your Own Action Filters
1. Add a new class to the Filters folder.
2. Give the class a name that ends in Attribute, such as LogAttribute.
3. Derive the class from ActionFilter.
4. Inside the class, override one of the four methods below to implement your own filtering. Depending on which method you override you can change the environment, results, or even the parameters of an action.
OnActionExecuted: Override to apply logic after an action executes.
OnActionExecuting: Override to look at a request and apply logic before an action executes.
OnResultExecuted: Override to apply logic after the action's result is executed. Can be used to run code after a view is rendered.
OnResultExecuting: Override to apply logic before the action's result is executed. Can be used to run code before a view is rendered.
5. Apply the filter either on an action, on a controller, or globally.
Custom Filter Example
Create the Action Filter Class
//(Filters > LogAttribute.cs) public class LogAttribute : ActionFilterAttribute { public override void OnActionExecuting(ActionExecutingContext filterContext) { // Do stuff here... base.OnActionExecuting(filterContext); } }
Apply to an Individual Action
//(Controllers > HomeController.cs) public class HomeController : Controller { // Note: Use Log, not LogAttribute [Log] public ActionResult About() { } }
Apply to an Entire Controller
// (Controllers > HomeController.cs) // Note: Use Log, not LogAttribute [Log] public class HomeController : Controller { // ... }
Apply Globally
// (App_Start > FilterConfig.cs) public class FilterConfig { public static void RegisterGlobalFilters(GlobalFilterCollection filters) { filters.Add(new HandleErrorAttribute()); // Note: Use LogAttribute here filters.Add(new LogAttribute()); } }