[ASP. NET topic] (7) -- filter in ASP. NET MVC

Source: Internet
Author: User
Document directory
  • 1. Filter Applied to action
  • Ii. Filter Applied to Controller
  • Iii. acceptverbs
  • Iv. actionname
  • V. nonaction
  • Vi. outputcache
  • VII. validateinput
  • 8. validateantiforgerytokenattribute

In ASP. net mvc applications, you can use the filter function when you have the following requirements:

Determine whether to log on or not or user permissions, determine the output cache, anti-leeching, anti-spider, local and international settings, and implement dynamic actions

Filter is a declarative programming method. In Asp.net MVC, it can only be limited to action (or its controller ).

Filter must inherit from the actionfilterattribute abstract class, and can overwrite void onactionexecuting (actionexecutingcontext) and
Void onactionexecuted (actionexecutedcontext), void onresultexecuting (resultexecutingcontext), and void onresultexecuted (resultexecutedcontext ).

Onactionexecuting is the operation before the action is executed, onactionexecuted is the operation after the action is executed, onresultexecuting is executed before the actionresult is parsed, and onresultexecuted is executed after the actionresult is parsed.

1. Filter Applied to action

The following example shows the execution sequence of the filter applied to action.

1. First, create a filter named testfilter. The Code is as follows:

Public class testfilter: actionfilterattribute <br/>{< br/> Public override void onactionexecuting (actionexecutingcontext filtercontext) <br/>{< br/> filtercontext. httpcontext. session ["Temp"] + = "testfilter onactionexecuting <br/>"; <br/>}< br/> Public override void onactionexecuted (actionexecutedcontext filtercontext) <br/>{< br/> filtercontext. httpcontext. session ["Temp"] + = "testfilter onactionexecuted <br/>"; <br/>}< br/> Public override void onresultexecuting (resultexecutingcontext filtercontext) <br/>{< br/> filtercontext. httpcontext. session ["Temp"] + = "testfilter onresultexecuting <br/>"; <br/>}< br/> Public override void onresultexecuted (resultexecutedcontext filtercontext) <br/>{< br/> filtercontext. httpcontext. session ["Temp"] + = "testfilter onresultexecuted <br/>"; <br/>}< br/>}

2. Create an action.

[Testfilter] // apply this filter to action <br/> Public actionresult filteraction () <br/>{< br/> return view (); <br/>}

3. Modify the corresponding view and write it in its view:

<% Session ["Temp"] + = "view execute <br/>"; %>

4. Finally, the output result of session ["Temp"] is displayed on other pages.

Testfilter onactionexecuting <br/> testfilter onactionexecuted <br/> testfilter onresultexecuting <br/> View execute <br/> testfilter onresultexecuted

The output result shows the execution sequence as shown above.

Ii. Filter Applied to Controller

There are two ways to apply the filter to the Controller:

1. Apply the filter directly to the Controller, for example:

[Testfilter] <br/> public class eicecontroller: controller <br/>{< br/>}

2. Rewrite the onactionexecuting, onactionexecuted, onresultexecuting, and onresultexecuted methods in the controller.

Finally, we will introduce the filters of several systems.

Iii. acceptverbs

Purpose: Specify the webpage ACCESS format.

[Acceptverbs (httpverbs. Post)] <br/> Public actionresult example () <br/>{< br/> return view (); <br/>}

The specified page can only be accessed in the form of post, that is, form submission.

Iv. actionname

Purpose: specify the action name.

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")] <br/> Public actionresult example (<br/>{< br/> return view (); <br/>}

V. nonaction

Purpose: It indicates that the current method is only a common method and is not parsed as an action.

Vi. outputcache

Purpose: Add cache for action.

[Outputcache (duration = 60, varybyparam = "*")] <br/> Public actionresult example () <br/>{< br/> return view (); <br/>}

VII. validateinput

Role: This action can accept HTML and other dangerous code (ASP. net mvc cannot complete equivalent tasks by setting the <% @ page attribute in Aspx .)

[Validateinput (false)] <br/> Public actionresult example () <br/>{< br/> return view (); <br/>}

8. validateantiforgerytokenattribute

Role: used to verify server tampering.

[Validateantiforgerytoken] <br/> Public actionresult example () <br/>{< br/> return view (); <br/>}

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.