asp.net MVC key Tutorial First anniversary version Sixth filters filter filter

Source: Internet
Author: User
Tags httpcontext

In ASP.NETMVC you can use the filter function when you have the following and similar requirements

Determine whether or not a login or user right

Decision output Caching

Anti-Theft chain

Anti-Spider

Localization and internationalization settings

Implementing Dynamic Action

Filter is a declarative programming method that can only be limited to an action (or its controller) in ASP.net mvc.

The filter is inherited from the ActionFilterAttribute abstract class and can override the void OnActionExecuting (ActionExecutingContext) and

void onactionexecuted (ActionExecutedContext)

and void Onresultexecuting (ResultExecutingContext) and

void onresultexecuted (ResultExecutedContext)

OnActionExecuting is the action that is performed before the action, and onactionexecuted is the actions performed by action

And onresultexecuting is to parse actionresult before execution, onresultexecuted is parse actionresult after execution.

The filter applied to action

Let me give you an example to see the order in which it is executed.

First we build a Filter, called 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]//将此Filter应用于Action
public  ActionResult filteraction()
{
return View();
}

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.