“ActionFilters” helps you to perform logic before a MVC action happens or after a MVC action is completed. Action filters are useful in the following scenarios:-
1. Implement pre-processing logic before the action happens.
2. Cancel a current execution.
3. Inspect the returned value.
4. Provide extra data to the action.
You can create action filters by two ways:-
· nline action filter.
· reating an “ActionFilter” attribute.
To create an inline action attribute we need to implement “IActionFilter” interface.The“IActionFilter” interface has two methods “OnActionExecuted” and “OnActionExecuting”. We can implement pre-processing logic or cancellation logic in these methods.
The problem with inline action attribute is that it cannot be reused across controllers. So we can convert the inline action filter to an action filter attribute. To create an action filter attribute we need to inherit from “ActionFilterAttribute” class and implement “IActionFilter” interface as shown in the below code.
Later we can decorate the controllers on which we want the action attribute to execute. You can see in the below code I have decorated the “Default1Controller” with “MyActionAttribute” class which was created in the previous code.
Also read ASP.NET interview question :- What is difference between “Server.Transfer” and “Response.Redirect”?
Do visit for more important MVC interview questions and answerat www.questpond.com
The above question is taken from the book .NET interview questions published by BPB publication and written by ShivprasadKoirala.The above question is taken from MVC chapter , you can see the complete ASP.NET MVC interview question from this chapter by clicking here.
