ASP. net mvc filter Overview

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.