Exemple #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.TryGet(controller, controller.Name, actionName, isPost);

                    var property = controller.GetProperty("HttpContext");

                    property.SetValue(routing.Instance, httpContext);

                    var args = nameValues.ToValArray();

                    var nargs = new object[] { httpContext };

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

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

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

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

                    if (routing.Atrrs != null && routing.Atrrs.Count > 0)
                    {
                        foreach (var arr in routing.Atrrs)
                        {
                            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));
                    }
                }
            }
        }
Exemple #2
0
        /// <summary>
        /// MVC处理
        /// </summary>
        /// <param name="httpContext"></param>
        /// <param name="controllerName"></param>
        /// <param name="actionName"></param>
        /// <param name="nameValues"></param>
        /// <param name="isPost"></param>
        /// <returns></returns>
        public static ActionResult Invoke(HttpContext httpContext, string controllerName, string actionName, NameValueCollection nameValues, bool isPost)
        {
            lock (_locker)
            {
                var first = _list.Where(b => b.Name.ToLower() == controllerName.ToLower()).FirstOrDefault();

                if (first == null)
                {
                    return(new ContentResult("找不到控制器", System.Net.HttpStatusCode.NotFound));
                }
                try
                {
                    var routing = RouteTable.TryGet(first, controllerName, actionName, isPost);

                    var property = first.GetProperty("HttpContext");

                    property.SetValue(routing.Instance, httpContext);

                    if (routing.Atrr != null)
                    {
                        var args = GetVals(nameValues);

                        var nargs = new object[] { httpContext };

                        var goOn = (bool)routing.Atrr.GetType().GetMethod("OnActionExecuting").Invoke(routing.Atrr, nargs.ToArray());

                        if (goOn)
                        {
                            var result = MethodInvoke(routing.Action, routing.Instance, nameValues);
                            nargs = new object[] { httpContext, result };
                            routing.Atrr.GetType().GetMethod("OnActionExecuted").Invoke(routing.Atrr, nargs);
                            return(result);
                        }
                        else
                        {
                            return(new ContentResult("当前逻辑已被拦截!", System.Net.HttpStatusCode.NotAcceptable));
                        }
                    }
                    return(MethodInvoke(routing.Action, routing.Instance, nameValues));
                }
                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));
                    }
                }
            }
        }