MVC Filter Basic use

Source: Internet
Author: User

Action Filter
/// <summary>///Execute before executing code/// </summary>/// <param name= "Filtercontext" ></param> Public Override voidonactionexecuting (ActionExecutingContext filtercontext) {//1. Display the name of the action method that triggered the filter    stringActionName =FilterContext.ActionDescriptor.ActionName; FilterContext.HttpContext.Response.Write ("actionname="+ ActionName +"</br>"); //2. Display the controller that triggered the filter action method    stringCtrlname =FilterContext.ActionDescriptor.ControllerDescriptor.ControllerName; FilterContext.HttpContext.Response.Write ("controllername="+ Ctrlname +"</br>"); //3. Determine if the current action method is affixed with the [HttpGet] attribute label    BOOLIsTrue = filterContext.ActionDescriptor.IsDefined (typeof(Httpgetattribute),false); FilterContext.HttpContext.Response.Write ("whether the current action is affixed with [HttpGet] attributes"+ IsTrue +"</br>"); //4. Get an instance with the [HttpGet] attribute label on the current action method    Object[] Httpgets = FilterContext.ActionDescriptor.GetCustomAttributes (typeof(Httpgetattribute),false); foreach(ObjectObjinchhttpgets) {FilterContext.HttpContext.Response.Write ("gets an instance of the current action method that is affixed with the [HttpGet] attribute label:"+ obj. ToString () +"</br>"); }//5. Record the actual parameters of the current action methodparameterdescriptor[] Dics =filterContext.ActionDescriptor.GetParameters (); foreach(Parameterdescriptor dicinchdics) {//Print the action parameter nameFilterContext.HttpContext.Response.Write ("record the actual parameters of the current action method:"+ DIC. Parametername.tostring () +"</br>"); }Base. OnActionExecuting (Filtercontext);}/// <summary>///Execute after executing code/// </summary>/// <param name= "Filtercontext" ></param> Public Override voidonactionexecuted (ActionExecutedContext filtercontext) {Base. OnActionExecuted (Filtercontext);}/// <summary>///execute before returning results/// </summary>/// <param name= "Filtercontext" ></param> Public Override voidonresultexecuting (ResultExecutingContext filtercontext) {Base. Onresultexecuting (Filtercontext);}/// <summary>///executes after returning the result/// </summary>/// <param name= "Filtercontext" ></param> Public Override voidonresultexecuted (ResultExecutedContext filtercontext) {Base. Onresultexecuted (Filtercontext);}

  Exception Filters:Handleerrorattribute
 Public Override void onexception (Exceptioncontext filtercontext) {    //1. Get exception object    Exception ex=  filtercontext.exception;     // 2. Write an exception to    the log // 2.1 The specific implementation process of writing to the log is slightly    ... // 3. Tell the MVC framework that the filter has handled the exception and that no additional processing    is required true ;     Base . Onexception (Filtercontext);}

  Configuration Error page:
1 <customErrorsMode= "On"defaultredirect= "Error">2     <ErrorStatusCode= "404"redirect= "~/errormgr/p404" />3     <ErrorStatusCode= "$"redirect= "~/errormgr/p500"/>4 </customErrors>

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.