When request processing is going on then I pass from different levels and sometimes it is required to pass some logic on a particular stage, then filters are used. So, basically filters are used to add some logic or injecting some logic at different levels when request is processed.
There are five types of filters with ASP.NET MVC 5 and they are as follows.
- Authorization Filter: It is performing before or after the validating the request.
- Action Filter: It is performing before or after action method.
- Result Filter: It is performing before or after the action result.
- Exception Filter: It is executed if there is some exception thrown by application when performing action process. It is used to show the error page or for logging error details.

Today, we are going to learn about only Action Filters and about rest of the filters we will cover in next article.
Action Filters
It is used to execute filter logic either before the action method execution or after the action method execution. It completes some task sometimes before the action runs or after the action run. There are several tasks which should be done before or after the action execution like logging, authentication, caching, etc.
To implement action filters, you need to create custom action filters.
Custom Action Filters
Now, you are going to create a Custom Action Filter which implements the pre-processing and post-processing logic. It will inherit from ActionFilterAttribute class and also implement IActionFilter interface.
- ActionFilterAttribute contains four important methods as in the following.
- OnActionExecuting: It is called just before the action method is going to call.
- OnActionExecuted: It is called just after the action method is called.
- OnResultExecuting: It is called just before the result is executed; it means before rendering the view.
- OnResultExecuted: It is called just after the result is executed, it means after rendering the view.

Now, it is time to add new class name as “CustomActionFilter.cs” which is inherited from ActionFilterAttribute and also implemented IActionFilter interface.
Let’s see an example of Custom Filter to check the user privilege to access the particular resource or not. See the following code.
Example 1
- using System.Web;
- using System.Web.Mvc;
- using Microsoft.AspNet.Identity;
- using System.Web.Routing;
- namespace FilterDemo
- {
- public class AuthorizationPrivilegeFilter : ActionFilterAttribute
- {
- public override void OnActionExecuting(ActionExecutingContext filterContext)
- {
- AuthorizationService _authorizeService = new AuthorizationService();
- string userId = HttpContext.Current.User.Identity.GetUserId();
- if (userId != null)
- {
- var result = _authorizeService.CanManageUser(userId);
- if (!result)
- {
- filterContext.Result = new RedirectToRouteResult(new RouteValueDictionary{{ "controller", "Account" },
- { "action", "Login" }
- });
- }
- }
- else
- {
- filterContext.Result = new RedirectToRouteResult(new RouteValueDictionary{{ "controller", "Account" },
- { "action", "Login" }
- });
- }
- base.OnActionExecuting(filterContext);
- }
- }
- }

Dev MahmodPosted Apr 4, 2021, 7:42 PM
What is AuthorizationService ? it give me compile error
Jean PaulPosted Sep 8, 2016, 5:40 PM
Nice, Thank you
Fatih CoskunPosted Dec 9, 2015, 1:23 PM
My MVC project example this : http://futbol1903.com/ what abour this
Aishwarya DPosted Dec 7, 2015, 12:41 AM
Good one
Santhakumar MunuswamyPosted Dec 4, 2015, 7:29 AM
Good one
sreenivasa kPosted Dec 2, 2015, 2:28 PM
good
Ravi PatelPosted Dec 2, 2015, 2:45 AM
very well explained sir, thanks for sharing