ASP. net mvc Filter

Source: Internet
Author: User

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

  1. Determine logon or User Permissions
  2. Decision output Cache
  3. Anti-leech
  4. Anti-spider
  5. Localization and internationalization settings
  6. Implement Dynamic Action

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)

And void OnResultExecuting (ResultExecutingContext) and
Void OnResultExecuted (ResultExecutedContext)

OnActionExecuting is the operation before the Action is executed, while 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 is an example of the execution sequence.
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/>";        }    }

 

Then create an Action:

[TestFilter] // apply this Filter to Action public ActionResult filteraction () {return View ();}

 

Write in its View:

<%Session["temp"] += "View Execute<br/>"; %>

 

Finally, the output result of Session ["temp"] is displayed on other pages:

TestFilter OnActionExecuting
TestFilter OnActionExecuted
TestFilter OnResultExecuting
View Execute
TestFilter OnResultExecuted

The execution sequence is also as follows:

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 the OnActionExecuting/OnActionExecuted/OnResultExecuting/OnResultExecuted methods in the Controller.

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();         }

 

Pages can only be accessed in the form of Post, that is, form submission.

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 dangerous code (ASP. net mvc cannot complete equivalent tasks by setting the <% @ Page attribute in aspx .)

        [ValidateInput(false)]        public ActionResult Example()        {            return View();        }

 

8. ValidateAntiForgeryTokenAttribute

Used to verify server tampering.

        [ValidateAntiForgeryToken]        public ActionResult Example()        {            return View();        }

Address: http://blog.bandao.cn/archive/28359/blogs-659434.aspx

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

  1. Determine logon or User Permissions
  2. Decision output Cache
  3. Anti-leech
  4. Anti-spider
  5. Localization and internationalization settings
  6. Implement Dynamic Action

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)

And void OnResultExecuting (ResultExecutingContext) and
Void OnResultExecuted (ResultExecutedContext)

OnActionExecuting is the operation before the Action is executed, while 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 is an example of the execution sequence.
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/>";        }    }

 

Then create an Action:

[TestFilter] // apply this Filter to Action public ActionResult filteraction () {return View ();}

 

Write in its View:

<%Session["temp"] += "View Execute<br/>"; %>

 

Finally, the output result of Session ["temp"] is displayed on other pages:

TestFilter OnActionExecuting
TestFilter OnActionExecuted
TestFilter OnResultExecuting
View Execute
TestFilter OnResultExecuted

The execution sequence is also as follows:

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 the OnActionExecuting/OnActionExecuted/OnResultExecuting/OnResultExecuted methods in the Controller.

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();         }

 

Pages can only be accessed in the form of Post, that is, form submission.

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 dangerous code (ASP. net mvc cannot complete equivalent tasks by setting the <% @ Page attribute in aspx .)

        [ValidateInput(false)]        public ActionResult Example()        {            return View();        }

 

8. ValidateAntiForgeryTokenAttribute

Used to verify server tampering.

        [ValidateAntiForgeryToken]        public ActionResult Example()        {            return View();        }

Address: http://blog.bandao.cn/archive/28359/blogs-659434.aspx

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.