Understanding Action Filters
Action filter is an attribute that can be applied to the controller action-or the entire controller, whose base class is System.Web.Mvc.FilterAttribute. It defines how the action executes. The ASP.net MVC framework contains several action filters.
Handleerror– This action filter handles errors that occur when controller action executes.
Outputcache– This action filter caches the output of controller action for a set time.
authorize– This action filter allows you to restrict access to specific users or roles.
Use Action Filter
Action filter is an attribute. You can apply most of the action filters to a single controller action or an entire controller.
For example, the following data controller has an index () method that returns the current time. This action has OutputCache action filter. This filter causes the value returned by the action to be cached for 10 seconds.
The settings used by the VaryByParam property do not recommend using all parameters to differentiate by setting the value of "*". This can cause a cache overflow.
Code highlighting produced by Actipro Codehighlighter (freeware)
http://www. codehighlighter.com/
-->public class Datacontroller:controller {////Get
:/data/
[ OutputCache (Duration = 20,varybyparam = "")] public
string Index ()
{return
DateTime.Now.ToString ();
}
}
If you repeatedly call the index () action (constantly refreshing the current page), you will see that the current content is unchanged in duration = 20 seconds.
A single action Filter–outputcache action filter– is applied to the index () method. Again, you can apply multiple action filters on the same action.
Different types of filters
The ASP.net MVC framework supports many different types of filters:
Authorization filters– implements Iauthorizationfilter characteristics.
The Action filters– implements the Iactionfilter feature.
Result filters– implement Iresultfilter characteristics.
Exception filters– implements Iexceptionfilter characteristics.
Filters are executed in the order listed above. For example, authorization filters always executes before action filters, exception filters after all other types of filter.