* 1: NetCore context, a global static package itself HttpContextHelper
* 2: configuration file read Json
{ // IConfiguration have the Configuration injection, two access nodes, colon .GetSection ( "the AppSettings:" + name) Configuration.GetSection ( "" ) .Value; }
* 3: Reading Session
// first download session expansion nuget package Microsoft.aspnetcore.http.extensions services.AddSession (); // write ConfigureServices in; app.UseSession (); // write Configure the HttpContextHelper.Current.Session.GetString ( " pwdqrcodekey " );
* 4: general fitter controller, inherited from: the Attribute, IActionFilter
/ **************** /
public class LoginFltAttribute : Attribute, IActionFilter { /// <summary> /// 执行后 /// </summary> /// <param name="context"></param> public void OnActionExecuted(ActionExecutedContext context) {} /// <summary> /// 执行前 /// </summary> /// <param name="context"></param> public void OnActionExecuting(ActionExecutingContext context) {} }
1. Gets the name of the similarities and differences of the current controller
// string actionName = filterContext.ActionDescriptor.ActionName; //MVC5写法 string actionName = filterContext.Controller.GetType().Name; //Core写法
-2 Returning to the similarities and differences between the data dictionary view
filterContext.Result = new ViewResult { ViewName = "error", ViewData = new ViewDataDictionary<Return_Msg>(msg) }; //MVC5写法 / * The ViewDataDictionary not instance, multi-flash monitoring using ViewData, he constructed, he can use the * / the ViewDataDictionary <Return_Msg> = ViewData new new the ViewDataDictionary <Return_Msg> (((the Controller) filterContext.Controller) .ViewData, MSG) ; filterContext.Result = new ViewResult() { ViewName = "error", ViewData = viewdata }; //Core的写法
-3. Attribute controller to get
filterContext.Controller.ViewBag.RightBtnView //MVC5写法 Controller controller = filterContext.Controller as Controller; controller.ViewBag.RightBtnView = btnView; //Core写法
-4. The controller acquires parameters (variable type is pDesc The ParameterDescriptor)
int zbid = Convert.ToInt32 (filterContext.ActionParameters [pDesc.ParameterName]); // MV5 wording int zbid = filterContext.ActionArguments [pDesc.Name] .ToString (); // Core wording filterContext.ActionDescriptor.Parameters. IList <The ParameterDescriptor> // set form storage filterContext.ActionDescriptor.Parameters [ 0 ] .Name // get the first name of the parameter, the parameter name can only get, get a value not corresponding to the key value filterContext.ActionArguments // dictionary form of argument list, you can get the key and value value, more flexible
-5. Get current list of parameters List
List < String > arrQueryKeys filterContext.HttpContext.Request.QueryString.AllKeys.ToList = (); // MV5 wording List < String > arrQueryKeys filterContext.HttpContext.Request.Form.Keys.ToList = (); // distal Form Form Parameter set KeyValuePair < String , Microsoft.Extensions.Primitives.StringValues> [] filterContext.HttpContext.Request.Query.ToArray KK = (); // parameter list address bar, where converted into an array, Core wording
-5.1 current list, get the specified parameters, to obtain the corresponding value with the specified key, keyName = specified key
int zbid= Convert.ToInt32(filterContext.HttpContext.Request.Params[keyName]); //MV5的写法 int zbid = Convert.ToInt32(filterContext.HttpContext.Request.Form.FirstOrDefault(i => i.Key == keyName).Value);
-6. To get access to the full address of the domain name last,
filterContext.HttpContext.Request.UrlReferrer.OriginalString; // head MVC5 determination request has no "Referer" is empty filterContext.HttpContext.Request.Headers [ " the Referer " ]; // Core
7. determines whether a request Ajax
filterContext.HttpContext.Request.IsAjaxRequest () // MC5 is determined whether the request is determined whether Ajax head has Head "X-Requested-With: XMLHttpRequest ", with a = Ajax request public static BOOL IsAjaxRequest ( the this the HttpRequest Request) { bool result = false; var xreq = request.Headers.ContainsKey("x-requested-with"); if (xreq) result = request.Headers["x-requested-with"] == "XMLHttpRequest"; return result; }
8. register. Already registered, you can get through this
protected virtual T CreateService<T>() { // typeof(T) ==>接口名 return (T)this.HttpContext.RequestServices.GetService(typeof(T)); }