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: