ASP.NET MVC provides a simple way to inject your piece of code or logic either before or after an action is executed. This can be achieved by using filters and attribute classes.
Types of Filters
The ASP.NET MVC framework provides five types of filters and executes in the same order as given below,
- Authentication filters
- Authorization filters
- Action filters
- Result filters
- Exception filters
Create an action method in HomeController and declare Attribute classes Above Action Method.
- public class HomeController: Controller
- {
- [CustomAuthorizationAttribute]
- [CustomActionAttribute]
- [CustomResultAttribute]
- [CustomExceptionAttribute]
- public ActionResult Index()
- {
- ViewBag.Message = "Index Action of Home controller is being called.";
- return View();
- }
- }
Now, Create a Filters Folder in your application and add the following attribute classes.
- CustomAuthorizationAttribute.cs
- public class CustomAuthorizationAttribute: FilterAttribute, IAuthorizationFilter
- {
- void IAuthorizationFilter.OnAuthorization(AuthorizationContext filterContext)
- {
- filterContext.Controller.ViewBag.OnAuthorization = "IAuthorizationFilter.OnAuthorization filter called";
- }
- }
- CustomActionAttribute.cs
- public class CustomActionAttribute: FilterAttribute, IActionFilter
- {
- void IActionFilter.OnActionExecuting(ActionExecutingContext filterContext)
- {
- filterContext.Controller.ViewBag.OnActionExecuting = "IActionFilter.OnActionExecuting filter called";
- }
- void IActionFilter.OnActionExecuted(ActionExecutedContext filterContext)
- {
- filterContext.Controller.ViewBag.OnActionExecuted = "IActionFilter.OnActionExecuted filter called";
- }
- }
- CustomResultAttribute.cs
- public class CustomResultAttribute: FilterAttribute, IResultFilter
- {
- void IResultFilter.OnResultExecuting(ResultExecutingContext filterContext)
- {
- filterContext.Controller.ViewBag.OnResultExecuting = "IResultFilter.OnResultExecuting filter called";
- }
- void IResultFilter.OnResultExecuted(ResultExecutedContext filterContext)
- {
- filterContext.Controller.ViewBag.OnResultExecuted = "IResultFilter.OnResultExecuted filter called";
- }
- }
- CustomExceptionAttribute.cs
- public class CustomExceptionAttribute: FilterAttribute, IExceptionFilter
- {
- void IExceptionFilter.OnException(ExceptionContext filterContext)
- {
- filterContext.Controller.ViewBag.OnException = "IExceptionFilter.OnException filter called";
- }
- }
View
Index.cshtml
- @{
- ViewBag.Title = "Index";
- }
- <h2>
- Index</h2>
- <ul>
- <li>@ViewBag.OnAuthorization</li>
- <li>@ViewBag.OnActionExecuting</li>
- <li>@ViewBag.OnActionExecuted</li>
- <li>@ViewBag.OnResultExecuting</li>
- <li>@ViewBag.OnResultExecuted</li>
- <li>@ViewBag.Message</li>
- </ul>
Here is the Output.


Mayank SharmaPosted Mar 21, 2017, 6:29 AM
Thank you everyone.
Mayank SharmaPosted Jun 28, 2016, 12:18 AM
Thank You Mr. Omid.
Omid NasriPosted Jun 27, 2016, 11:00 AM
Good One.
Mayank SharmaPosted Jun 27, 2016, 12:10 AM
Please see one of my article I posted from another account . This will help you for sure. http://www.c-sharpcorner.com/blogs/how-to-pass-data-from-view-to-controller-in-asp-net-mvc
Mayank SharmaPosted Jun 27, 2016, 12:07 AM
Thank you each and everyone. Your comments are valuable.
Prasanna MuraliPosted Jun 25, 2016, 6:42 AM
Nice one..
Debasis SahaPosted Jun 25, 2016, 6:02 AM
Good One..
Former memberPosted Jun 25, 2016, 12:53 AM
Nice Article....
kalu singh raoPosted Jun 25, 2016, 12:03 AM
Nice...
Bhuvanesh MohankumarPosted Jun 24, 2016, 12:42 PM
Good one
Nikhil SanganiPosted Jun 24, 2016, 8:31 AM
Nice Article Sir.
Mayank SharmaPosted Jun 24, 2016, 4:52 AM
Thank You Sir, I appreciate.
Sanjay SabariyaPosted Jun 24, 2016, 4:50 AM
Good one.
Mayank SharmaPosted Jun 24, 2016, 3:07 AM
Thank You.
MannanPosted Jun 24, 2016, 2:47 AM
nice article ...keep it up.. bro