In ASP.NET MVC, controllers will be defining the action methods and these action methods will usually have a one-to-one relationship with UI controls, like clicking a button or a link, etc. For example, in earlier examples, UserController class contained methods UserAdd, UserDelete, etc.
Though, many times we like performing some action before or after a particular operation. To achieve this functionality, ASP.NET MVC provides a feature for adding pre- and post-action behaviours on the controller's action methods.
ASP.NET MVC framework supports below action filters
Action filters are one of the most frequently used filters for performing additional data processing, or to manipulate return values or to cancel the execution of action or to modify the view structure at run time.
Action Filters are additional attributes applied to either a controller section or the entire controller for modifying the way in which an action is executed. These attributes are special .NET classes derived from System.Attribute which is attached to classes, methods, properties, and fields.
ASP.NET MVC provides below action filters
Now, let’s see the code example to apply these filters on an example controller ActionFilterDemoController. (ActionFilterDemoController is just used as an example and these filters can be used on any of the controllers.)
Example − Specifies the return value to be cached for 10 seconds.
Example – Redirects the application to a custom error page when an error is triggered by the controller.
With the above code, if any error occurs while the action execution, it will find a view named Error in the Views folder and render that page to the user.
Example − Allowing only authorized users to log in the application.
With the above code, if the user tries accessing the application without logging in, it will throw an error similar to the one shown in below screenshot.
MVC Framework Related Interview Questions
|. NET Interview Questions||Framework7 Interview Questions|
|Java collections framework Interview Questions||Microsoft Solutions Framework (MSF) Interview Questions|
|Resource Description Framework (RDF) Interview Questions||Unittest Framework Interview Questions|
|Spring MVC Framework Interview Questions||Dot Net Framework Interview Questions|
|Net Micro Framework Interview Questions|
MVC Framework Related Practice Tests
|. NET Practice Tests||Resource Description Framework (RDF) Practice Tests|
|Spring MVC Framework Practice Tests||Dot Net Framework Practice Tests|
Mvc Framework Tutorial
All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd
Wisdomjobs.com is one of the best job search sites in India.