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, 2:29 AM EDT
Thank you everyone.
Mayank SharmaPosted Jun 27, 2016, 8:18 PM EDT
Thank You Mr. Omid.
Omid NasriPosted Jun 27, 2016, 7:00 AM EDT
Good One.
Mayank SharmaPosted Jun 26, 2016, 8:10 PM EDT
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 26, 2016, 8:07 PM EDT
Thank you each and everyone. Your comments are valuable.
Prasanna MuraliPosted Jun 25, 2016, 2:42 AM EDT
Nice one..
Debasis SahaTOP 100Posted Jun 25, 2016, 2:02 AM EDT
Good One..
Former memberPosted Jun 24, 2016, 8:53 PM EDT
Nice Article....
kalu singh raoPosted Jun 24, 2016, 8:03 PM EDT
Nice...
Bhuvanesh MohankumarPosted Jun 24, 2016, 8:42 AM EDT
Good one
Nikhil SanganiPosted Jun 24, 2016, 4:31 AM EDT
Nice Article Sir.
Mayank SharmaPosted Jun 24, 2016, 12:52 AM EDT
Thank You Sir, I appreciate.
Sanjay SabariyaPosted Jun 24, 2016, 12:50 AM EDT
Good one.
Mayank SharmaPosted Jun 23, 2016, 11:07 PM EDT
Thank You.
MannanPosted Jun 23, 2016, 10:47 PM EDT
nice article ...keep it up.. bro