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>