Exemplo n.º 1
0
        /// <summary>
        /// MVC处理
        /// </summary>
        /// <param name="httpContext"></param>
        /// <param name="controller"></param>
        /// <param name="actionName"></param>
        /// <param name="nameValues"></param>
        /// <param name="isPost"></param>
        /// <returns></returns>
        public static ActionResult Invoke(HttpContext httpContext, Type controller, string actionName, NameValueCollection nameValues, bool isPost)
        {
            lock (_locker)
            {
                try
                {
                    var routing = RouteTable.GetOrAdd(controller, controller.Name, actionName, isPost);

                    if (routing == null)
                    {
                        throw new Exception($"当前请求为:{(isPost ? "HttpPOST" : "HttpGET")} 找不到:{controller.Name}/{actionName}");
                    }

                    routing.Instance.HttpContext = httpContext;

                    var nargs = new object[] { httpContext };

                    if (routing.FilterAtrrs != null && routing.FilterAtrrs.Count > 0)
                    {
                        foreach (var arr in routing.FilterAtrrs)
                        {
                            var goOn = (bool)FastInvoke.GetMethodInvoker(arr.GetType().GetMethod("OnActionExecuting")).Invoke(arr, nargs.ToArray());

                            if (!goOn)
                            {
                                return(new ContentResult("当前逻辑已被拦截!", System.Net.HttpStatusCode.NotAcceptable));
                            }
                        }
                    }

                    if (routing.ActionFilterAtrrs != null && routing.ActionFilterAtrrs.Count > 0)
                    {
                        foreach (var arr in routing.ActionFilterAtrrs)
                        {
                            var goOn = (bool)FastInvoke.GetMethodInvoker(arr.GetType().GetMethod("OnActionExecuting")).Invoke(arr, nargs.ToArray());

                            if (!goOn)
                            {
                                return(new ContentResult("当前逻辑已被拦截!", System.Net.HttpStatusCode.NotAcceptable));
                            }
                        }
                    }

                    var result = MethodInvoke(routing.Action, routing.ActionInvoker, routing.Instance, nameValues);

                    nargs = new object[] { httpContext, result };

                    if (routing.FilterAtrrs != null && routing.FilterAtrrs.Count > 0)
                    {
                        foreach (var arr in routing.FilterAtrrs)
                        {
                            FastInvoke.GetMethodInvoker(arr.GetType().GetMethod("OnActionExecuted")).Invoke(arr, nargs);
                        }
                    }

                    if (routing.ActionFilterAtrrs != null && routing.ActionFilterAtrrs.Count > 0)
                    {
                        foreach (var arr in routing.FilterAtrrs)
                        {
                            FastInvoke.GetMethodInvoker(arr.GetType().GetMethod("OnActionExecuted")).Invoke(arr, nargs);
                        }
                    }
                    return(result);
                }
                catch (Exception ex)
                {
                    if (ex.Message.Contains("找不到此action"))
                    {
                        return(new ContentResult(ex.Message, System.Net.HttpStatusCode.NotFound));
                    }
                    else
                    {
                        return(new ContentResult(ex.Message, System.Net.HttpStatusCode.InternalServerError));
                    }
                }
            }
        }