MVC filter usage and MVC filter usage

Source: Internet
Author: User

MVC filter usage and MVC filter usage

 

Instance product based on asp.net mvc 5.0 framework, source code: http://www.jinhusns.com/Products/Download

 

 

In Asp.net Mvc, you can use the Filter function when you have the following and similar requirements:

  • Determine logon or User Permissions
  • Decision output Cache
  • Anti-leech
  • Anti-spider
  • Localization and internationalization settings
  • Implement Dynamic Action

Asp. Net MVC provides the following default filters:

Note that actionfilteratrifilter provided by Asp. Net MVC implements IActionFilter and IResultFilter by default. ActionFiterAttribute is an Abstract type, so it cannot be used directly because it cannot be instantiated. Therefore, to use ActionFiterAttribute, you must inherit it before using it.
Filters inherit from ActionFiterAtribute abstract classes and can overwrite void OnActionExecuting (ActionExecutingContext), void OnActionExecuted (ActionExecutedContext), void aggregate (ResultExecutingContext), and aggregate (ResultExecutedContext ).
Their execution order is as follows:

OnActionExecuting is the operation before the Action is executed.
OnActionExecuted indicates the operation after the Action is executed.
OnResultExecuting is executed before parsing ActionResult
OnResultExecuted is executed after the ActionResult is parsed.

Here, we only talk about a common operation before the Action is executed to verify whether the user is logged on or whether the permission has passed.
The following is the filter rewrite method: Message is a parameter (optional)

Then there is the method of using the Action. Add the filter mark on the Action and add parameters, as shown below:

Then, access the preceding Action in the browser to see the following execution sequence:

 

Related Article

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.