Javatpoint Logo
Javatpoint Logo

ASP.NET MVC Action Filters

The MVC framework provides the filter attribute so that we can filter the user requests. We can apply it to an individual action or an entire controller. It completely modified the way in which an action is called.

The ASP.NET MVC Framework provides the following action filters.

OutputCache: it makes controller's action output cacheable for the specified time.

HandleError: It is used to handle error raised when a controller action executes.

Authorize: It allows only authorize user to access resources.


OutputCache Example

Here, we are implementing OutputCache that will cache the action method output for the specified time. The action code is given below:

// MusicStoreController.cs

It renders an index file that has the following code.

// index.cshtml

Output:

It produces the following output and cache that for 10 seconds. It will not change before 10 seconds even we refresh the web page again and again.

ASP Action filters 1

Authorize Example

Now, we are applying authorize attribute to the action method, the code is given below.

// MusicStoreController.cs

Output:

This attribute will restrict unauthorized user access. The application redirects to the login page for authentication.

ASP Action filters 2
Next TopicASP.NET MVC Model





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA