MVC filter OnActionExecuting () Get the trigger controller, Action, etc in the filter, onactionexecuting
<1>
Using System; using System. collections. generic; using System. linq; using System. web; using System. web. mvc; namespace MVC filter. filters {// custom a filter [MyActionFilter] public class MyActionFilterAttribute: ActionFilterAttribute {// override OnActionExecuting method public override void OnActionExecuting (ActionExecutingContext filterContext) {// Let's first take a look at the filterContext parameter: we know that the OnActionExecuting method is a method that will be triggered before the Action is executed, which means that, In the future, I will write code here, I want to know whether your OnActionExecuting method is triggered when the Action is called (because the OnActionExecuting filter method is triggered when all action methods are executed, so I want to know which action is triggered when it is executed) // obtain the name of the Action that triggers the current method (OnActionExecuting) (I .e: the OnActionExecuting (ActionExecutingContext filterContext) string actionName = filterContext triggered when the Action method is executed. actionDescriptor. actionName; // get the Controller name of the Action that triggers the current method string controllerName = filterCo Ntext. actionDescriptor. controllerDescriptor. controllerName; // obtain all the parameters of the Action method that triggers the current method (because there may be multiple parameters, it is a set and its return type is IDictionary <string, object> In the following example, var is used instead of var paramss = filterContext. actionParameters; string str = ""; if (paramss. any () // Any is used to determine whether the set contains Any element. If the contained element is returned, true is returned. Otherwise, false {foreach (var key in paramss. keys) // traverses its key (because we want to obtain the parameter name s, so the traversal key) {str = key + "is" + paramss [key]; // paramss [key] Yeskey value }}// obtain the context filterContext of the current request. httpContext. response. write ("Hello, me"); // Replace the returned result view of the Action method that triggers the current method with a JsonResult (filterContext. the return type of Result is JsonResult.) // filterContext. result: gets or sets the Result returned by the operation method. (Since it is to obtain or set the returned results of the Action method, we can tamper with the returned results of the Action method that triggers the current method here. // example: the Action method that triggers the current method is: public ActionResult Add () {return Content ("China ");} the return value of this Action method is a "Chinese" text, so we can use filterContext here. result to tamper with its return value. For example, I will return a json JsonResult json = new JsonResult (); json. data = new {status = "1", message = "OK"}; json. jsonRequestBehavior = JsonRequestBehavior. allowGet; filterContext. result = json; // obtain the region var area = filterContext. routeData. dataTokens; // MVC can have a region, where the region is stored // obtain the region name var areaName = area ["area"]; // This ["area"] is completely written. You can obtain the region name based on ["area"], because the key of the region is always area // RouteData var rd = filterContext. routeData; // you can obtain the control name, ation name, and parameter name var controlName = rd. values ["Controller"]. toString (); var actName = rd. values ["Action"]. toString ();}}}
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.