/// <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); }
/// <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> 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="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)); } } }