/// <summary> /// MVC处理 /// </summary> /// <param name="httpContext"></param> /// <param name="routeTable"></param> /// <param name="controller"></param> /// <param name="actionName"></param> /// <param name="nameValues"></param> /// <param name="isPost"></param> /// <returns></returns> public static ActionResult InvokeResult(HttpContext httpContext, RouteTable routeTable, Type controller, string actionName, NameValueCollection nameValues, bool isPost) { if (routeTable == null) { return(new ContentResult($"o_o,当前未注册任何Controller!", System.Net.HttpStatusCode.NotFound)); } var routing = routeTable.GetOrAdd(controller, actionName, isPost); if (routing == null) { return(new ContentResult($"o_o,找不到:{controller.Name}/{actionName} 当前请求为:{(isPost ? ConstHelper.HTTPPOST : ConstHelper.HTTPGET)}", System.Net.HttpStatusCode.NotFound)); } routing.Instance.HttpContext = httpContext; var nargs = new object[] { httpContext }; //类过滤器 if (routing.FilterAtrrs != null && routing.FilterAtrrs.Any()) { foreach (var arr in routing.FilterAtrrs) { var method = arr.GetType().GetMethod(ConstHelper.ONACTIONEXECUTING); if (method != null) { var goOn = (bool)method.Invoke(arr, nargs.ToArray()); if (!goOn) { return(new ContentResult("o_o,当前逻辑已被拦截!", System.Net.HttpStatusCode.NotAcceptable)); } } } } //方法过滤器 if (routing.ActionFilterAtrrs != null && routing.ActionFilterAtrrs.Any()) { foreach (var arr in routing.ActionFilterAtrrs) { if (arr.ToString() == ConstHelper.OUTPUTCACHEATTRIBUTE) { var method = arr.GetType().GetMethod(ConstHelper.ONACTIONEXECUTING); if (method != null) { httpContext.Session.CacheCalcResult = (string)arr.GetType().GetMethod(ConstHelper.ONACTIONEXECUTING).Invoke(arr, nargs.ToArray()); } } else { var method = arr.GetType().GetMethod(ConstHelper.ONACTIONEXECUTING); if (method != null) { var goOn = (bool)arr.GetType().GetMethod(ConstHelper.ONACTIONEXECUTING).Invoke(arr, nargs.ToArray()); if (!goOn) { return(new ContentResult("o_o,当前逻辑已被拦截!", System.Net.HttpStatusCode.NotAcceptable)); } } } } } #region actionResult ActionResult result; if (httpContext.Request.ContentType == ConstHelper.FORMENCTYPE3 && !string.IsNullOrEmpty(httpContext.Request.Json)) { var nnv = SerializeHelper.Deserialize <Dictionary <string, string> >(httpContext.Request.Json).ToNameValueCollection(); result = MethodInvoke(routing.Action, routing.Instance, nnv); } else { result = MethodInvoke(routing.Action, routing.Instance, nameValues); } #endregion nargs = new object[] { httpContext, result }; if (routing.FilterAtrrs != null && routing.FilterAtrrs.Any()) { foreach (var arr in routing.FilterAtrrs) { var method = arr.GetType().GetMethod(ConstHelper.ONACTIONEXECUTED); if (method != null) { method.Invoke(arr, nargs); } } } if (routing.ActionFilterAtrrs != null && routing.ActionFilterAtrrs.Any()) { foreach (var arr in routing.ActionFilterAtrrs) { if (arr.ToString() == ConstHelper.OUTPUTCACHEATTRIBUTE) { continue; } else { var method = arr.GetType().GetMethod(ConstHelper.ONACTIONEXECUTED); if (method != null) { method.Invoke(arr, nargs); } } } } return(result); }
/// <summary> /// MVC处理 /// </summary> /// <param name="routeTable"></param> /// <param name="controller"></param> /// <param name="actionName"></param> /// <param name="nameValues"></param> /// <param name="isPost"></param> /// <returns></returns> public static ActionResult InvokeResult(RouteTable routeTable, Type controller, string actionName, NameValueCollection nameValues, bool isPost) { if (routeTable == null) { return(new ContentResult($"o_o,当前未注册任何Controller!", System.Net.HttpStatusCode.NotFound)); } var routing = routeTable.GetOrAdd(controller, actionName, isPost); if (routing == null) { return(new ContentResult($"o_o,找不到:{controller.Name}/{actionName} 当前请求为:{(isPost ? ConstHelper.HTTPPOST : ConstHelper.HTTPGET)}", System.Net.HttpStatusCode.NotFound)); } ActionResult result; //类过滤器 if (routing.FilterAtrrs != null && routing.FilterAtrrs.Any()) { foreach (var arr in routing.FilterAtrrs) { var method = arr.GetType().GetMethod(ConstHelper.ONACTIONEXECUTING); if (method != null) { var goOn = (bool)method.Invoke(arr, null); if (!goOn) { return(new ContentResult("o_o,拒绝访问!", System.Net.HttpStatusCode.Forbidden)); } } } } //方法过滤器 if (routing.ActionFilterAtrrs != null && routing.ActionFilterAtrrs.Any()) { foreach (var arr in routing.ActionFilterAtrrs) { if (arr.ToString() == ConstHelper.OUTPUTCACHEATTRIBUTE) { var method = arr.GetType().GetMethod(ConstHelper.ONACTIONEXECUTING); if (method != null) { HttpContext.Current.Session.CacheCalcString = (string)arr.GetType().GetMethod(ConstHelper.ONACTIONEXECUTING).Invoke(arr, null); if (HttpContext.Current.Session.CacheCalcString.IndexOf("1,") == 0) { return(new ContentResult(string.Empty, System.Net.HttpStatusCode.NotModified)); } } } else { var method = arr.GetType().GetMethod(ConstHelper.ONACTIONEXECUTING); if (method != null) { var goOn = (bool)arr.GetType().GetMethod(ConstHelper.ONACTIONEXECUTING).Invoke(arr, null); if (!goOn) { return(new ContentResult("o_o,拒绝访问!", System.Net.HttpStatusCode.Forbidden)); } } } } } #region actionResult if (HttpContext.Current.Request.ContentType == ConstHelper.FORMENCTYPE3 && !string.IsNullOrEmpty(HttpContext.Current.Request.Json)) { try { var nnv = SerializeHelper.Deserialize <Dictionary <string, string> >(HttpContext.Current.Request.Json).ToNameValueCollection(); result = MethodInvoke(routing.Action, routing.Instance, nnv); } catch { return(new ContentResult("o_o,错误请求,Json:" + HttpContext.Current.Request.Json, System.Net.HttpStatusCode.BadRequest)); } } else { result = MethodInvoke(routing.Action, routing.Instance, nameValues); } #endregion var nargs = new object[] { result }; if (routing.FilterAtrrs != null && routing.FilterAtrrs.Any()) { foreach (var arr in routing.FilterAtrrs) { var method = arr.GetType().GetMethod(ConstHelper.ONACTIONEXECUTED); if (method != null) { method.Invoke(arr, nargs); } } } if (routing.ActionFilterAtrrs != null && routing.ActionFilterAtrrs.Any()) { foreach (var arr in routing.ActionFilterAtrrs) { if (arr.ToString() == ConstHelper.OUTPUTCACHEATTRIBUTE) { continue; } else { var method = arr.GetType().GetMethod(ConstHelper.ONACTIONEXECUTED); if (method != null) { method.Invoke(arr, nargs); } } } } return(result); }
public IHttpResult GetActionResult(HttpContext httpContext) { RouteTable routeTable = (RouteTable)this.Parma; string url = httpContext.Request.Url; bool isPost = httpContext.Request.Method == ConstHelper.POST; //禁止访问 var flist = httpContext.WebConfig.ForbiddenAccessList; if (flist.Any()) { foreach (var item in flist) { if (url.IndexOf(item, StringComparison.OrdinalIgnoreCase) >= 0) { return(new ContentResult("o_o,当前内容禁止访问!", System.Net.HttpStatusCode.Forbidden)); } if (System.Text.RegularExpressions.Regex.IsMatch(url, item, System.Text.RegularExpressions.RegexOptions.IgnoreCase)) { return(new ContentResult("o_o,当前内容禁止访问!", System.Net.HttpStatusCode.Forbidden)); } } } var arr = url.Split("/", StringSplitOptions.RemoveEmptyEntries); var filePath = string.Empty; NameValueCollection nameValues; switch (arr.Length) { case 0: filePath = httpContext.Server.MapPath(httpContext.WebConfig.HomePage); if (StaticResourcesCache.Exists(filePath)) { return(new FileResult(filePath, httpContext.IsStaticsCached)); } else { var d = RouteTable.Types.Where(b => (string.Compare(b.Name, httpContext.WebConfig.DefaultRoute.Name, true) == 0 || string.Compare(b.Name, httpContext.WebConfig.DefaultRoute.Name + ConstHelper.CONTROLLERNAME, true) == 0)).FirstOrDefault(); nameValues = httpContext.Request.Parmas.ToNameValueCollection(); return(MvcInvoke(httpContext, routeTable, d, httpContext.WebConfig.DefaultRoute.Value, nameValues, isPost)); } case 1: filePath = httpContext.Server.MapPath(url); if (StaticResourcesCache.Exists(filePath)) { return(new FileResult(filePath, httpContext.IsStaticsCached)); } break; default: var controllerName = arr[arr.Length - 2]; var first = RouteTable.Types.Where(b => string.Compare(b.Name, controllerName + ConstHelper.CONTROLLERNAME, true) == 0).FirstOrDefault(); if (first != null) { nameValues = httpContext.Request.Parmas.ToNameValueCollection(); return(MvcInvoke(httpContext, routeTable, first, arr[arr.Length - 1], nameValues, isPost)); } else { filePath = httpContext.Server.MapPath(url); if (StaticResourcesCache.Exists(filePath)) { return(new FileResult(filePath, httpContext.IsStaticsCached)); } } break; } return(new ContentResult("o_o,找不到任何内容", System.Net.HttpStatusCode.NotFound)); }
/// <summary> /// mvc HttpContext /// </summary> /// <param name="webHost"></param> /// <param name="httpMessage"></param> public HttpContext(IWebHost webHost, HttpMessage httpMessage) : base(webHost, httpMessage) { _routeTable = _webHost.RouteParam as RouteTable; }
/// <summary> /// mvc HttpContext /// </summary> /// <param name="webHost"></param> public HttpContext(IWebHost webHost) : base(webHost) { _routeTable = _webHost.RouteParam as RouteTable; }
/// <summary> /// MVC处理 /// </summary> /// <param name="routeTable"></param> /// <param name="controller"></param> /// <param name="actionName"></param> /// <param name="nameValues"></param> /// <param name="isPost"></param> /// <returns></returns> public static ActionResult InvokeResult(RouteTable routeTable, Type controller, string actionName, NameValueCollection nameValues, bool isPost) { if (routeTable == null) { return(new ContentResult($"o_o,当前未注册任何Controller!", System.Net.HttpStatusCode.NotFound)); } var routing = routeTable.GetOrAdd(controller, actionName, isPost); if (routing == null) { return(new ContentResult($"o_o,找不到:{controller.Name}/{actionName} 当前请求为:{(isPost ? ConstHelper.HTTPPOST : ConstHelper.HTTPGET)}", System.Net.HttpStatusCode.NotFound)); } ActionResult result; var goOn = true; //类过滤器 if (routing.FilterAtrrs != null && routing.FilterAtrrs.Any()) { foreach (var arr in routing.FilterAtrrs) { var method = arr.GetType().GetMethod(ConstHelper.ONACTIONEXECUTING); if (method != null) { goOn = (bool)method.Invoke(arr, null); } } } //方法过滤器 if (routing.ActionFilterAtrrs != null && routing.ActionFilterAtrrs.Any()) { foreach (var arr in routing.ActionFilterAtrrs) { var method = arr.GetType().GetMethod(ConstHelper.ONACTIONEXECUTING); if (method != null) { if ((bool)arr.GetType().GetMethod(ConstHelper.ONACTIONEXECUTING).Invoke(arr, null) == false) { goOn = false; } } } } #region actionResult if (goOn) { if (!string.IsNullOrEmpty(HttpContext.Current.Request.ContentType) && HttpContext.Current.Request.ContentType.IndexOf(ConstHelper.FORMENCTYPE3, StringComparison.InvariantCultureIgnoreCase) > -1 && !string.IsNullOrEmpty(HttpContext.Current.Request.Json)) { try { var dic = SerializeHelper.Deserialize <Dictionary <string, string> >(HttpContext.Current.Request.Json); if (HttpContext.Current.Request.Parmas == null || !HttpContext.Current.Request.Parmas.Any()) { HttpContext.Current.Request.Parmas = dic; } else { if (dic != null && dic.Any()) { foreach (var item in dic) { HttpContext.Current.Request.Parmas[item.Key] = item.Value; } } } nameValues = HttpContext.Current.Request.Parmas.ToNameValueCollection(); } catch { return(new ContentResult("o_o,错误请求,Json:" + HttpContext.Current.Request.Json, System.Net.HttpStatusCode.BadRequest)); } } result = MethodInvoke(routing.Action, routing.Instance, nameValues); } else { result = new EmptyResult(); } #endregion var nargs = new object[] { result }; if (routing.FilterAtrrs != null && routing.FilterAtrrs.Any()) { foreach (var arr in routing.FilterAtrrs) { var method = arr.GetType().GetMethod(ConstHelper.ONACTIONEXECUTED); if (method != null) { method.Invoke(arr, nargs); result = (ActionResult)nargs[0]; } } } if (routing.ActionFilterAtrrs != null && routing.ActionFilterAtrrs.Any()) { foreach (var arr in routing.ActionFilterAtrrs) { var method = arr.GetType().GetMethod(ConstHelper.ONACTIONEXECUTED); if (method != null) { method.Invoke(arr, nargs); result = (ActionResult)nargs[0]; } } } return(result); }
/// <summary> /// MVC处理 /// </summary> /// <param name="httpContext"></param> /// <param name="routeTable"></param> /// <param name="controller"></param> /// <param name="actionName"></param> /// <param name="nameValues"></param> /// <param name="isPost"></param> /// <returns></returns> private static ActionResult MvcInvoke(HttpContext httpContext, RouteTable routeTable, Type controller, string actionName, NameValueCollection nameValues, bool isPost) { try { var routing = routeTable.GetOrAdd(controller, actionName, isPost); if (routing == null) { throw new Exception($"o_o,找不到:{controller.Name}/{actionName} 当前请求为:{(isPost ? ConstHelper.HTTPPOST : ConstHelper.HTTPGET)}"); } routing.Instance.HttpContext = httpContext; var nargs = new object[] { httpContext }; if (routing.FilterAtrrs != null && routing.FilterAtrrs.Count > 0) { foreach (var arr in routing.FilterAtrrs) { var method = arr.GetType().GetMethod(ConstHelper.ONACTIONEXECUTING); if (method != null) { var goOn = (bool)method.Invoke(arr, nargs.ToArray()); if (!goOn) { return(new ContentResult("o_o,当前逻辑已被拦截!", System.Net.HttpStatusCode.NotAcceptable)); } } } } if (routing.ActionFilterAtrrs != null && routing.ActionFilterAtrrs.Count > 0) { foreach (var arr in routing.ActionFilterAtrrs) { if (arr.ToString() == ConstHelper.OUTPUTCACHEATTRIBUTE) { var method = arr.GetType().GetMethod(ConstHelper.ONACTIONEXECUTING); if (method != null) { httpContext.Session.CacheCalcResult = (string)arr.GetType().GetMethod(ConstHelper.ONACTIONEXECUTING).Invoke(arr, nargs.ToArray()); } } else { var method = arr.GetType().GetMethod(ConstHelper.ONACTIONEXECUTING); if (method != null) { var goOn = (bool)arr.GetType().GetMethod(ConstHelper.ONACTIONEXECUTING).Invoke(arr, nargs.ToArray()); if (!goOn) { return(new ContentResult("o_o,当前逻辑已被拦截!", System.Net.HttpStatusCode.NotAcceptable)); } } } } } var result = MethodInvoke(routing.Action, routing.Instance, nameValues); nargs = new object[] { httpContext, result }; if (routing.FilterAtrrs != null && routing.FilterAtrrs.Count > 0) { foreach (var arr in routing.FilterAtrrs) { var method = arr.GetType().GetMethod(ConstHelper.ONACTIONEXECUTED); if (method != null) { method.Invoke(arr, nargs); } } } if (routing.ActionFilterAtrrs != null && routing.ActionFilterAtrrs.Count > 0) { foreach (var arr in routing.ActionFilterAtrrs) { if (arr.ToString() == ConstHelper.OUTPUTCACHEATTRIBUTE) { continue; } else { var method = arr.GetType().GetMethod(ConstHelper.ONACTIONEXECUTED); if (method != null) { method.Invoke(arr, nargs); } } } } return(result); } catch (Exception ex) { if (ex.Message.Contains("o_o,找不到")) { return(new ContentResult(ex.Message, System.Net.HttpStatusCode.NotFound)); } else { return(new ContentResult("→_→,出错了:" + ex.Message, System.Net.HttpStatusCode.InternalServerError)); } } }