Action and filter
 
 
 
In Asp.net MVC, filter can only be restricted to action and controller. It inherits from actionfilterattribute and can overwrite the following important methods.
1: void onactionexecuting (actionexecutingcontext): action before execution
 
 
 
2: void onactionexecuted (actionexecutedcontext): The operation after the action is executed.
 
 
 
3: void onresultexecuting (resultexecutingcontext): executed before parsing actionresult
 
 
 
4: void onresultexecuted (resultexecutedcontext): After parsing the actionresult, execute
 
 
A common filter provided by the system:
1: acceptverbs
2: actionname, both of which have restrictions on Action access conditions;
3: outputcache: Set the cache;
4: validateinput, added data verification.
 
 
 
Example:
1: The message book list data can be written in this way, or [acceptverbs (httpverbs. Get)] can be omitted. It is the default value. This type of access is in the form of a link.
 
   [Acceptverbs (httpverbs. Get)]
   Public  Actionresult index ()
{
VaR Models   =   Inter. findallinfo ();
   Return   View (   "   Index   "   , Models );
}
  
 
2: when creating a message: we use httpverbs. Post. In this case, the corresponding action must be executed only after the form is submitted.
 
   [Acceptverbs (httpverbs. Post)]
   Public   Actionresult create (guestbookinfo Model)
{
   Try   
{
Inter. Add (model );
VaR Models  =   Inter. findallinfo ();
Tempdata [   "   Tempdata   "   ]   =       "   Created successfully   "   ;
   Return   Redirecttoaction (  "   Index   "   );
}
   Catch   (Exception ex)
{
Modelstate. addmodelerror (   "   Ex   "   , Ex );
  Return  View (model );
 
 
 
}
}
 
 
3: The actionnameattribute usage is similar to the acceptverbsattribute method. If no actionname is specified, the system will find the view with the same name and method name by default.
 
  [Actionname (  "  Edit  "  )]
  Public Actionresult edit (  Int  ID) 
 
Custom filter:
Create a noclientcacheattribute without client cache. You must inherit actionfilterattribute and override the onactionexecuting method.
 
    Public     Class Noclientcacheattribute: actionfilterattribute
{
  Public     Override     Void  Onactionexecuting (actionexecutingcontext filtercontext)
{
Httpcontext. Current. response. cachecontrol  =     "  No-Cache " ;
}
 
 
 
}
 
 
It is especially simple to use in action, just as it is in C.
 
  [Noclientcache]
  Public  Actionresult details (  Int  ID)
 
 
Acceptverbs working mechanism.
 
1. acceptverbsattribute class. It inherits actionmethodselectorattribute, and the important method is to override the isvalidforrequest method of the base class, which is also a filter function.
 
  [Attributeusage (attributetargets. method, allowmultiple  =  False  , Inherited  =  True  )]
  Public     Sealed    Class  Acceptverbsattritor: actionmethodselectorattribute
{
  //  Fields  
  [Compilergenerated]
  Private  Icollection  <  String  >     <  Verbs > K _ backingfield;
 
 
 //  Methods  
    Public  Acceptverbsattribute (httpverbs verbs );
  Public  Acceptverbsattribute (  Params     String  [] Verbs );
 Private     Static     Void  Addentrytolist (httpverbs verbs, httpverbs match, list  <  String  >  Verblist,  String    
 entrytext); 
  internal     static     string   [] enumtoarray (httpverbs verbs); 
   Public     override     bool   isvalidforrequest (controllercontext, methodinfo);  
//Properties
PublicIcollection<String>Verbs {[compilergenerated]Get;Private[Compilergenerated]Set;}
}
 
 
 
 
Second: isvalidforrequest method:
 
  Public     Override     Bool  Isvalidforrequest (controllercontext, methodinfo)
{
  If  (Controllercontext  =     Null )
{
  Throw     New  Argumentnullexception (  "  Controllercontext  "  );
}
  String  Httpmethod  =  Controllercontext. httpcontext. Request. httpmethod;
 Return     This  . Verbs. Contains  <  String  >  (Httpmethod, stringcomparer. ordinalignorecase );
}
 
 
We can see that this. verbs is a set of request. httpmethod in this method. How is it initialized? Let's look at the constructor of acceptverbsattribute. It also calls another constructor public acceptverbsattribute (Params string [] verbs)
 
 
 
  Public Acceptverbsattribute (httpverbs verbs ):  This  (Enumtoarray (verbs ))
{
} 
 
The following enumtoarray method and addentrytolist method are important. The enumtoarray method is used to collect all request. httpmethod. The addentrytolist method is used to compare the current access type with all request. httpmethod and obtain their intersection.
 
 
 
 
 Internal     Static     String [] Enumtoarray (httpverbs verbs)
{
List  <  String  >  Verblist  =     New  List  <  String  >  ();
Addentrytolist (verbs, httpverbs. Get, verblist,  "  Get "  );
Addentrytolist (verbs, httpverbs. Post, verblist,  "  Post  "  );
Addentrytolist (verbs, httpverbs. Put, verblist,  "  Put  "  );
Addentrytolist (verbs, httpverbs. Delete, verblist,  "  Delete  " );
Addentrytolist (verbs, httpverbs. Head, verblist,  "  Head  "  );
  Return  Verblist. toarray ();
} 
 
 
 
 
 
 
 
 
  Private     Static    Void  Addentrytolist (httpverbs verbs, httpverbs match, list  <  String  >  Verblist,  String  Entrytext)
{
  If  (Verbs  &  Match)  ! =     0 )
{
Verblist. Add (entrytext );
}
} 
 
Looking back at the isvalidforrequest method, it is easy to understand why this filer can be used for filtering.
 
 
Summary:Using the filter feature, we can write some extended content, such as compressing the page's attivity, and non-cached attivity.
Note: Reference: http://www.rainsts.net/article.asp? Id = 786