ASP. net mvc Action Method

Source: Internet
Author: User

Sometimes you want to process some logic before or after calling the Action method. To support this, ASP. net mvc allows you to create an Action filter. The Action filter is a custom Attributes used to mark the actions before or after the Action method is added to the Action Method in the Controller class.

Some places that may use the Action filter include:
◆ Log, Exception Handling
◆ Authentication and authorization-restrict user access
◆ Output cache-Save the result of an Action
◆ Web crawler Filtering
◆ Localization
◆ Dynamic Action-inject an Action into the Controller
◆ ASP. net mvc provides the following Filter interfaces:


IActionFilter
IAuthorizationFilter
IExceptionFilter
IResultFilter

To implement a Filter, We need to inherit from the FilterAttribute class and simultaneously implement one or more of the above interfaces:

 
 
  1. public class MyFilter : FilterAttribute, IActionFilter, IResultFilter{  

OnActionExecuting is called before the Action method is executed, and OnActionExecuted is called after the Action method is executed. Note that their parameters are ActionExecutingContext and ActionExecutedContext.

The ActionExecutedContext class contains a Canceled attribute, which allows you to cancel the current Action (why is the Canceled attribute in ActionExecutingContext in P3 lost in P5? Magic. How can I cancel an Action during OnActionExecuting ?).

The FilterExcutedContext class contains an Exception attribute and an ExceptionHandled attribute. If the Exception attribute is null, no Exception is found in action stack, indicating that the Action method has not encountered an error. Otherwise, an exception occurs. If you set the ExceptionHandled attribute to true, it indicates that an exception has been processed in the Filter.

They are executed before and after the return result (for example, return View ();) of the Action. It is similar to IActionFilter.

IAuthorizationFilter is a Filter used for identity authentication. Only one void OnAuthorization (AuthorizationContext filterContext) method is provided.

IExceptionFilter is called when an exception occurs. It also provides only one void OnException (ExceptionContext filterContext) method;

The above describes the Action method of ASP. net mvc.

  1. Introduction to ASP. NET 2.0 Virtual Hosts
  2. Introduction to ASP. NET Applications
  3. Optimize ASP. NET 2.0 Profile Provider
  4. ASP. NET pipeline Optimization
  5. Introduction to ASP. NET Routing Engine

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.