First, create a filter named testfilter.
Public class testfilter: actionfilterattribute
{
Public override void onactionexecuting (actionexecutingcontext filtercontext)
{
Filtercontext. httpcontext. session ["Temp"] + = "testfilter onactionexecuting <br/> ";
}
Public override void onactionexecuted (actionexecutedcontext filtercontext)
{
Filtercontext. httpcontext. session ["Temp"] + = "testfilter onactionexecuted <br/> ";
}
Public override void onresultexecuting (resultexecutingcontext filtercontext)
{
Filtercontext. httpcontext. session ["Temp"] + = "testfilter onresultexecuting <br/> ";
}
Public override void onresultexecuted (resultexecutedcontext filtercontext)
{
Filtercontext. httpcontext. session ["Temp"] + = "testfilter onresultexecuted <br/> ";
}
}
1. Filter Applied to action
[Testfilter] // apply this filter to action
Public actionresult filteraction ()
{
Return view ();
}
Ii. Filter of Controller
There are two ways to apply the filter to the Controller:
1. Apply the filter directly to the Controller, for example:
[Testfilter]
Public class eicecontroller: Controller
{
}
2. Rewrite
Onactionexecuting/onactionexecuted/onresultexecuting/onresultexecuted.
The following describes the filters of several systems:
Iii. acceptverbs
Specifies the access form of the page, such
[Acceptverbs (httpverbs. Post)]
Public actionresult example (){
Return view ();
}
Iv. actionname
Specifies the name of the action.
Application Scenario: if you do not want to use the method name as the action name or the action name as the keyword, for example
[Actionname ("class")]
Public actionresult example (){
Return view ();
}
V. nonaction
The current method is only a common method and is not parsed as an action.
Vi. outputcache
Add cache for action
[Outputcache (duration = 60, varybyparam = "*")]
Public actionresult example ()
{
Return view ();
}
VII. validateinput
This action can accept HTML and other dangersCode(Asp.net MVC sets the <% @ page attribute in aspx to fail to complete the equivalent task .)
[Validateinput (false)]
Public actionresult example ()
{
Return view ();
}
8. validateantiforgerytokenattribute
Used to verify server tampering.
[Validateantiforgerytoken]
Public actionresult example ()
{
Return view ();
}
Address: http://www.25175.com/200609/25175/25175_html/2010-07/3537.html