Beispiel #1
0
        public static object ExecuteAction(TrafficContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }
            InvokeInfo vkInfo = ReflectionHelper.GetActionInvokeInfo(context.Request.CommandId);

            if (vkInfo == null)
            {
                return(null);
            }
            var controller = vkInfo.Instance as IMyController;

            controller.UserData = context.Request.UserData;
            // 调用方法
            object result = ExecuteActionInternal(context, vkInfo);
            // 设置OutputCache
            // 处理方法的返回结果
            IActionResult executeResult = result as IActionResult;

            if (executeResult != null)
            {
                executeResult.Ouput(context);
                return(executeResult);
            }
            else
            {
                return(result);
            }
        }
Beispiel #2
0
        ///// <summary>
        ///// 根据要调用的方法名返回对应的 Action
        ///// </summary>
        ///// <param name="controller"></param>
        ///// <param name="action"></param>
        ///// <param name="request"></param>
        ///// <returns></returns>
        //private static ActionDescription GetServiceAction(Type controller, string action, HttpRequest request)
        //{
        //    if (controller == null)
        //        throw new ArgumentNullException("controller");
        //    if (string.IsNullOrEmpty(action))
        //        throw new ArgumentNullException("action");

        //    // 首先尝试从缓存中读取
        //    string key = request.HttpMethod + "#" + controller.FullName + "@" + action;
        //    ActionDescription mi = (ActionDescription)s_ServiceActionTable[key];

        //    if (mi == null)
        //    {
        //        bool saveToCache = true;

        //        MethodInfo method = FindAction(action, controller, request);

        //        if (method == null)
        //        {
        //            // 如果Action的名字是submit并且是POST提交,则需要自动寻找Action
        //            // 例如:多个提交都采用一样的方式:POST /Ajax/Product/submit
        //            if (action.IsSame("submit") && request.HttpMethod.IsSame("POST"))
        //            {
        //                // 自动寻找Action
        //                method = FindSubmitAction(controller, request);
        //                saveToCache = false;
        //            }
        //        }

        //        if (method == null)
        //            return null;

        //        var attr = method.GetMyAttribute<ActionAttribute>();
        //        mi = new ActionDescription(method, attr);

        //        if (saveToCache)
        //            s_ServiceActionTable[key] = mi;
        //    }

        //    return mi;
        //}

        //private static MethodInfo FindAction(string action, Type controller, HttpRequest request)
        //{
        //    foreach (MethodInfo method in controller.GetMethods())
        //    {
        //        if (method.Name.IsSame(action))
        //        {
        //            if (MethodActionIsMatch(method, request))
        //                return method;
        //        }
        //    }
        //    return null;
        //}

        //private static MethodInfo FindSubmitAction(Type controller, HttpRequest request)
        //{
        //    string[] keys = request.Form.AllKeys;

        //    foreach (MethodInfo method in controller.GetMethods())
        //    {
        //        string key = keys.FirstOrDefault(x => method.Name.IsSame(x));
        //        if (key != null && MethodActionIsMatch(method, request))
        //            return method;
        //    }

        //    return null;
        //}

        //private static bool MethodActionIsMatch(MethodInfo method, HttpRequest request)
        //{
        //    var attr = method.GetMyAttribute<ActionAttribute>();
        //    if (attr != null)
        //    {
        //        if (attr.AllowExecute(request.HttpMethod))
        //            return true;
        //    }
        //    return false;
        //}

        /// <summary>
        /// 根据一个页面请求路径,返回内部表示的调用信息。
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public static InvokeInfo GetActionInvokeInfo(string commandid)
        {
            if (commandid == "")
            {
                throw new ArgumentNullException("url");
            }

            ActionDescription action = null;

            if (s_PageActionDict.TryGetValue(commandid, out action) == false)
            {
                return(null);
            }

            InvokeInfo vkInfo = new InvokeInfo();

            vkInfo.Controller = action.PageController;
            vkInfo.Action     = action;

            if (vkInfo.Action.MethodInfo.IsStatic == false)
            {
                vkInfo.Instance = Activator.CreateInstance(vkInfo.Controller.ControllerType);
            }

            return(vkInfo);
        }
Beispiel #3
0
        internal static object ExecuteActionInternal(TrafficContext context, InvokeInfo info)
        {
            // 准备要传给调用方法的参数
            object[] parameters = GetActionCallParameters(context, info.Action);

            // 调用方法
            if (info.Action.HasReturn)
            {
                return(info.Action.MethodInfo.Invoke(info.Instance, parameters));
            }

            else
            {
                info.Action.MethodInfo.Invoke(info.Instance, parameters);
                return(null);
            }
        }