Filters in Asp.net MVC

. Saturday, January 10, 2015
1 comments

Hello Friends,

Today We're going to discuss What are the Filters,How many types they are and How do they Work, so without any further Ado lets start.

If you want to perform Your Stuff "Pre" or "Post" Action then use Filters. They Work in similar way as there are various Filters Implemented in any Water Filter that's why They are called Filters :).

Lets look at the diagram below
 
This is how the Filters are Arranged in the Asp.net MVC Pipeline. The Arrangement shown in the Diagram is same as Arrangement in MVC i.e. each one is being kept in the sequence as per its Work.
 
Now You've the Basic Idea about Filters in MVC, Now we need to explore each one of them, Lets start with Authorize Filter First.
 
I think I've explained everything related to authorize Filter so in Brief You're action can be Executed only by the Users, Roles to whom You've granted the Permission.
 
Now Lets dig into Next and very Important ActionFilter. There are actually two methods Representing This First OnActionExecuting This one fires as soon as Authorization has been taken place but Action doesn't start Executing i.e. "Before" Action and another one is OnActionExecuted this one fires as soon as Action has executed all of Its Code and and before Its Ready to return the Contents to View i.e. "After" Action Execution.
Lets see the diagram
 

 
Once these two filters has been Executed Control leaves the coding Part Action,Controller and Now Its Time to Render the Contents to related View.The Two Filters responsible for this are OnResultExecuting and OnResultExecuted Respectively.Please note that OnResultExecuting is something where You can make manipulation in the Contents which will be rendered in the View whereas OnResultExecuted is something where You can release the Resources,finish the tasks,Log the Changes etc.
 
 
Apart from this one more is there Exception Filter which is inherited from IExceptionFilter and Implements OnException which will occur whenever there's any error occur during Execution of any Filter. Please note that even if there's error in any of the Filter the Sequential Execution of Filters will Remain means They will execute their Code but the Actual Exception can only be caught at Exception Level Filter.
 
 
In How Many Ways You can apply the Filters in MVC ?
well, There're mainly Four Ways You can do this
 
1)Decorate the inbuilt Filters given by System.web.mvc upon any Action.
e.g.
 
[Authorize(User="UserName",Roles="Specify one or more roles separated by comma"]
Public ActionResult Index()
 
2)Use Custom Filters i.e. Create various Classes and Inherit them as follows :
       a)If Its AuthorizeFilter then IAuthorizeFilter,Implements OnAuthorization
       b)If Its ActionFilter then ActionFilterAttribute which implements 
       OnActionExecuting,OnActionExecuted.
       c)If Its ResultFilter then again the mentioned above but Implement
       OnResultExecuting,OnResultExecuted.
      d)If Its ExceptionFilter then IExceptionFilter,Implements OnException.
3)Registering Filter using Filters.FilterProvider for specificity.
4)Registering Global Filters.
 
Orders and Scope of the Filters
 
a)Order :
Its an Attribute which you can specify to determine Execution Order of Similar Type of Filters.
by default Filters Decorated on any Action arranged as Stack in which the Top one has the Less value than the bottom one i.e. -1 so lesser Order value Filter Executes First,You can explicitly specify this while decorating.
 
 
b)Scope :
What If two same filter type have same Order ? then the Scope comes in,following is their precedence
 
First
Global
Controller
Action
Last


 
 
Cancellation :

Suppose there are two different Filters A,B, placed A above B. each one Implements are the basic four type of Filters.
so below will be their Executing Sequence.

OnAuthorization - A
OnAuthorization - B
OnActionExecuting - A
OnActionExecuting - B
OnActionExecuted - B
OnActionExecuted - A
OnResultExecuting - A
OnResultExecuting - B
OnResultExecuted - B
OnResultExecuted - A
OnException - A
OnException - B

Now Let Say You want to suspend the Executing of A during the Process then You can do this by setting the Result Property to non null value which will abort the upcoming events of similar type for this Filter and Other Filters but any pending or Already Executed Filter's events will be Executed.
so lets say you set Result in OnActionExecuting - A to any string value then the above sequence will be
OnAuthorization - A
OnAuthorization - B
OnActionExecuting - A
All the Upcoming Implementation of similar Types will be aborted
OnResultExecuting - A
OnResultExecuting - B
and so on....

So That's It from my side, Hope You've enjoyed the Article If you have any queries in this You can ask it.
Complete Project for the above explanation can be downloaded from Here.