1.

What are Action filters in ASP.NET MVC?

Answer»

ACTION filters are the additional ATTRIBUTES in the ASP.NET MVC framework, that we can apply to either a section of the CONTROLLER or to the entire controller for modifying the approach in which action will be executed. Action filter attributes are SAID to be special .NET classes as they are derived from the System.Attribute class and they can be ATTACHED to classes, properties, methods, and fields.

The following action filters are provided by ASP.NET MVC:

  • Output Cache: This action filter will cache the controller action output for a specified time.
  • Handle Error: This action filter will handle the errors raised during the execution of a controller action.
  • Authorize: This action filter will enable you for restricting to a particular role or user.

The below-given code will apply Output cache filter on an ActionFilterExampleController (Here, ActionFilterExampleController is just used as an example, You can apply action filters on any of the controllers). It specifies that the return value should be cached for 20 seconds.

public class ActionFilterExampleController: Controller{ [HttpGet] OutputCache(Duration = 20)] public string Index() { return DateTime.Now.ToString("T"); }}


Discussion

No Comment Found