ASP. NET MVC Filter (iv) preface
In the previous article about the Iactionfilter method execution filter in the framework of the implementation of the process is described, this article will be iactionfilter type of filter used to do some introduction.
ASP . NET MVC
Filter Filters
- The overall object model of the filter in the system frame
- Iauthorizationfilter Authorization Authentication filter Execution Process
- Use Iauthorizationfilter Filter Filters
- Iactionfilter the execution process of the behavior filter
- Custom Implementations I Actionfilter Behavior Filter
- Use of Exception filters
Custom implementation Iactionfilter Behavior filter
Or then the previous controller space example to demonstrate that no one has seen the friend also does not matter, as long as there is a basic display page on it.
Figure 1
This is the initial page called the Iocdemocontroller Controller default index method.
Let's implement our custom Iactionfilter type.
Code 1-1
1 Public classMycustomactionfilterattribute:filterattribute, Iactionfilter2 {3 4 Public voidonactionexecuted (actionexecutedcontext filtercontext)5 {6FilterContext.HttpContext.Response.Write ("This is the controller method:"+ FilterContext.ActionDescriptor.ActionName +"-After execution");7 8 }9 Ten Public voidonactionexecuting (actionexecutingcontext filtercontext) One { AFilterContext.HttpContext.Response.Write ("This is the controller method:"+ FilterContext.ActionDescriptor.ActionName +"-Before execution"); - } -}
In the previous article we have described the above code in the design to the type structure, which is not repeated here, we have defined the behavior filter then use it.
After applying this filter, let's look at the results of the run:
Figure 2
We can try to put the filter on the controller behavior to be executed, not the controller:
Code 1-2
1 Public classIocdemocontroller:controller2 {3 PrivateIdatastandard _datastandard;4 5 PublicIocdemocontroller (Idatastandard datastandard)6 {7_datastandard =Datastandard;8 }9 Ten [Mycustomactionfilter] One Publicactionresult Index () A { - returnView (_datastandard.getproducts ()); - the } -}
Running the discovery again will be the same as the result on your controller. The only difference is that the scope of application of the filter is different, when placed on the method only when the controller behavior is called to call these filters, and placed on the controller of the filter, no matter what you request the controller of the behavior will be called.
The order in which the filters are executed
Through the previous studies, we can see from the framework of hard coding, controller of different types of execution order, in turn, authorization authentication filter, behavior filter, result filter. There is also an exception filter, which is unrestricted and triggers the call whenever an exception occurs. Here is the order of the frame calls before the different types of filters, so how do we control the order of calls between the same type filters? See Example:
Code 1-3
1 Public classMycustomactionfileteroneattribute:filterattribute, Iactionfilter2 {3 4 Public voidonactionexecuted (actionexecutedcontext filtercontext)5 {6FilterContext.HttpContext.Response.Write ("this is"+ This. GetType (). Name +"filters in the Controller method:"+ FilterContext.ActionDescriptor.ActionName +"-Output after execution");7 }8 9 Public voidonactionexecuting (actionexecutingcontext filtercontext)Ten { OneFilterContext.HttpContext.Response.Write ("this is"+ This. GetType (). name+"filters in the Controller method:"+ FilterContext.ActionDescriptor.ActionName +"-Output before execution"); A } -}
And the output example in the above 1-1 code is modified to 1-3 such an input sample, and then we add the new defined behavior filter to the behavior method:
1 [Mycustomactionfilter]2 [Mycustomactionfileterone]3 public ActionResult Index ()4 {5 return View (_datastandard.getproducts ()); 6 }
Let's take a look at the results
Figure 3
See this result, some friends may say, is in accordance with the order of the post-add location to execute, so let's say, we will modify the index () method to add the two filters in the attribute variable order, this property to the Imvcfilter interface type, All filter types are implemented, except that the FilterAttribute type in our customizations has been implemented for us.
Take a look at the modified sample code:
Code 1-4
1 [Mycustomactionfilter (order=2)] 2 [Mycustomactionfileterone (order=1)] 3 Public actionresult Index () 4 {5 return View (_datastandard.getproducts ()); 6 }
Run results
There are a number of ways to use filters, as explained in this article's Iactionfilter type filter.
Jinyuan
Source: http://www.cnblogs.com/jin-yuan/
This article is copyrighted by the author and the blog Park, welcome reprint, but without the consent of the author must retain this statement, and on the article page