action filter in mvc

sam
sam
134 Points
62 Posts

Hi,

I am working on MVC. What are action filter? How can we create custom action filter?

Views: 8185
Total Answered: 2
Total Marked As Answer: 0
Posted On: 04-Jan-2016 05:13

Share:   fb twitter linkedin
Answers
Brian
Brian
Participant
570 Points
75 Posts
         

Hi Sam,

Action filters are attributes that you can put on Action or entire controller. Action filters modify the way in which Action executing. MVC provides different inbuilt Action Filter as

  • OutputCache – caches the output of a controller action for a specified amount of time.
  • HandleError – handles errors raised when a controller action executes.
  • Authorize – enables you to restrict access to a particular user or role.

You can also create you own Action Filter. For suppose you want to create custom authentication system or log the user activity or going to modify the response content (minify the response contents) etc. In all cases you can create you custom Action Filter.

For create a new custom Filter you need to know different type of Action Filters:

  1. Authentication filters - Implements the IAuthenticationFilter attribute.
  2. Authorization filters – Implements the IAuthorizationFilter attribute.
  3. Action filters – Implements the IActionFilter attribute.
  4. Result filters – Implements the IResultFilter attribute.
  5. Exception filters – Implements the IExceptionFilter attribute.

 Filters are executed in the order listed above.

The base class of action filters is the System.Web.Mvc.FilterAttribute. If you want to implement a filter, then you need to create a class that inherits from the FilterAttribute class and implements one or more of the IAuthenticationFilterIAuthorizationFilterIActionFilter, IResultFilter, or ExceptionFilter interfaces.

Posted On: 04-Jan-2016 05:39
Rahul Maurya
Rahul M...
Moderator
1172 Points
211 Posts
         
Posted On: 18-Feb-2016 08:32
 Log In to Chat