In Asp. netMvc, 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
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:
- Determine logon or User Permissions
- Decision output Cache
- Anti-leech
- Anti-spider
- Localization and internationalization settings
- 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