Tip: ASP. NET MVC4: (6) Global filter, asp. netmvc4

Source: Internet
Author: User

Tip: ASP. NET MVC4: (6) Global filter, asp. netmvc4

Original article: http://blog.csdn.net/zx13525079024/article/details/19161777

The global filter in Asp. Net MVC4 can monitor the whole project globally.

Create an MVC4 project. You can see the following code in the global. asax file: FilterConfig. RegisterGlobalFilters (GlobalFilters. Filters );

Indicates registering a global filter.

GlobalFilters is a set of global filters. You can add a filter using the add method. By default, the HandleErrorAttribute filter is added to the set.

Next, create a custom filter and add it to the global filter set.

1. Create a custom filter

To create a custom filter, you must inherit the ActionFilterAttribute class. We create a filter named mermerfilterattribute and record the time in the action.

The Code is as follows:

 

[Csharp]View plaincopy
  1. Public class CustomerFilterAttribute: ActionFilterAttribute
  2. {
  3. Public override void OnActionExecuting (ActionExecutingContext filterContext)
  4. {
  5. Base. OnActionExecuting (filterContext );
  6. FilterContext. HttpContext. Response. Write ("Start Time:" + DateTime. Now. ToString () + "<br/> ");
  7. }
  8. Public override void OnActionExecuted (ActionExecutedContext filterContext)
  9. {
  10. Base. OnActionExecuted (filterContext );
  11. Var controllerName = filterContext. RouteData. Values ["controller"]. ToString ();
  12. Var actionName = filterContext. RouteData. Values ["action"]. ToString ();
  13. FilterContext. HttpContext. Response. Write ("End Time:" + DateTime. Now. ToString () + "<br/> ");
  14. FilterContext. HttpContext. Response. Write ("controller:" + controllerName + ", action:" + actionName );
  15. }
  16. }

2. Register a global Filter

After the filter is created, we Add the filter to the global filter and use filters. Add (new CustomerFilterAttribute (); method,

The Code is as follows:

[Csharp]View plaincopy
  1. Public class FilterConfig
  2. {
  3. Public static void RegisterGlobalFilters (GlobalFilterCollection filters)
  4. {
  5. Filters. Add (new HandleErrorAttribute ());
  6. Filters. Add (new CustomerFilterAttribute ());
  7. }
  8. }

Next we run each page of the project, and we will see the output time and controller name in the page, as shown below:




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.