---
title: "What are Action Filter in asp.net MVC?"  
description: "What are Action Filter in asp.net MVC?"  
author: "Kalin"  
published: 2022-03-11  
updated: 2023-06-04  
canonical: https://www.mindstick.com/forum/156911/what-are-action-filter-in-asp-dot-net-mvc  
category: "asp.net mvc"  
tags: ["asp.net", "asp.net mvc"]  
reading_time: 3 minutes  

---

# What are Action Filter in asp.net MVC?

What are [Action](https://www.mindstick.com/forum/155752/what-is-action-result-with-its-types) [Filter in asp.net](https://www.mindstick.com/forum/156904/what-is-authorization-filter-in-asp-dot-net-mvc) [MVC](https://www.mindstick.com/forum/155803/define-cache-profile-in-mvc)?

## Replies

### Reply by Aryan Kumar

In [ASP.NET](https://www.mindstick.com/articles/934/default-folders-available-inside-the-asp-dot-net-application-folder) MVC, action filters are classes that allow you to add functionality to controller actions before or after they are executed. They are implemented as attributes that can be applied to controller actions or controllers.

Action filters can be used to perform a variety of tasks, such as:

- **Logging:** Action filters can be used to log information about controller actions, such as the time they were executed, the parameters they were passed, and the results they returned.
- **Caching:** Action filters can be used to cache the results of controller actions, which can improve performance by reducing the number of times the actions need to be executed.
- **Authorization:** Action filters can be used to authorize access to controller actions, which can help to protect your application from unauthorized users.
- **Validation:** Action filters can be used to validate the parameters that are passed to controller actions, which can help to ensure that the data is valid before it is processed.

Action filters are a powerful tool that can be used to improve the security, performance, and functionality of your ASP.NET MVC applications.

Here are some examples of built-in action filters in ASP.NET MVC:

- **Authorize:** This [filter](https://www.mindstick.com/forum/1176/filter-in-a-xml-file-in-c-sharp) can be used to restrict access to a controller action to users who are authenticated and have the appropriate permissions.
- **HandleError:** This filter can be used to handle errors that occur in controller actions.
- **OutputCache:** This filter can be used to cache the output of a controller action for a specified amount of time.

You can also create your own custom action filters to perform custom tasks. To do this, you need to create a class that inherits from the ActionFilterAttribute class. The ActionFilterAttribute class provides two methods that you can override:

- **OnActionExecuting**: This method is called before the controller action is executed.
- **OnActionExecuted**: This method is called after the controller action has been executed.

In these methods, you can add any custom logic that you want to execute before or after the controller action is executed.

Action filters are a powerful tool that can be used to improve the security, performance, and functionality of your ASP.NET MVC applications. If you are not already using action filters, I encourage you to learn more about them and start using them in your applications.

### Reply by Steilla Mitchel

**[Action Filter](https://www.mindstick.com/blog/11275/action-filter-in-asp-dot-net-mvc) in [asp.net MVC](https://www.mindstick.com/forum/155798/what-is-caching-in-asp-dot-net-mvc) :**

Action filter are also called before executing the action method and after the action method has been executed. An Action Filters are additional attribute that can be applied either with controller section or with the entire controller to modify the way in which action is executed. It has two method that are used in Action filter which are,

**1-** OnAtionExecuted.

**2**- OnActioExeturing.

**Exp.**

```
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace Mindstick.Filters
{
public class MyactionfilterDemo : FilterAttribute,IActionFilter
{
public void OnActionExecuted(ActionExecutedContext filterContext)
{
if (filterContext.HttpContext.Session['UserID'] != null)
{
filterContext.Result = new RedirectResult('/Home/Index');
}
else
{
filterContext.Result = new RedirectResult('/Login /Login');
}
}
public void OnActionExecuting(ActionExecutingContext filterContext)
{
if (filterContext.HttpContext.Session['UserID'] != null)
{
filterContext.Result = new RedirectResult('/Home/ Index');
}
else
{
filterContext.Result = new RedirectResult('/Login /Login');
}
}
}
}
[MyactionfilterDemo]
[AcceptVerbs(HttpVerbs.Get)]
public ActionResult GetPersonData()
{
PersonTest p = new PersonTest ();
return View('PersonTest',p);
}
```

\


---

Original Source: https://www.mindstick.com/forum/156911/what-are-action-filter-in-asp-dot-net-mvc

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
