MVC Framework Action Filters - MVC Framework

What is the use of Action filters in MVC framework?

In ASP.NET MVC, controllers will be defining the action methods and these action methods will usually have a one-to-one relationship with UI controls, like clicking a button or a link, etc. For example, in earlier examples, UserController class contained methods UserAdd, UserDelete, etc.

Though, many times we like performing some action before or after a particular operation. To achieve this functionality, ASP.NET MVC provides a feature for adding pre- and post-action behaviours on the controller's action methods.

Types of Filters

ASP.NET MVC framework supports below action filters

  • Action Filters − Action filters are used for implementing logic which is executed before and after a controller action executes.
  • Authorization Filters − Authorization filters are used for implementing authentication and authorization for controller actions.
  • Result Filters − Result filters contain logic which is executed before and after a view result is executed. For example, if you want to modify a view result right before the view is rendered to the browser.
  • Exception Filters − Exception filters are the last type of filters to run. User can use an exception filter for handling errors raised by either the controller actions or controller action results. Exception filters can be used to log errors.

Action filters are one of the most frequently used filters for performing additional data processing, or to manipulate return values or to cancel the execution of action or to modify the view structure at run time.

Action Filters

Action Filters are additional attributes applied to either a controller section or the entire controller for modifying the way in which an action is executed. These attributes are special .NET classes derived from System.Attribute which is attached to classes, methods, properties, and fields.

ASP.NET MVC provides below action filters

  • Output Cache − Output Cache action filter caches the output of a controller action for a specific amount of time.
  • Handle Error − Handle Error action filter will handle the errors which are raised when a controller action executes.
  • Authorize − Authorize action filter will enable the user in restricting access to a particular user or role.

Now, let’s see the code example to apply these filters on an example controller ActionFilterDemoController. (ActionFilterDemoController is just used as an example and these filters can be used on any of the controllers.)

Output Cache

Example − Specifies the return value to be cached for 10 seconds.

Handle Error

Example – Redirects the application to a custom error page when an error is triggered by the controller.

With the above code, if any error occurs while the action execution, it will find a view named Error in the Views folder and render that page to the user.

Authorize

Example − Allowing only authorized users to log in the application.

With the above code, if the user tries accessing the application without logging in, it will throw an error similar to the one shown in below screenshot.

filter

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

MVC Framework Topics