Example #1
0
        /// <summary>
        /// 获取动作的参数
        /// </summary>
        /// <param name="controllerContext"></param>
        /// <param name="actionDescriptor"></param>
        /// <returns></returns>
        protected virtual IDictionary <string, object> GetParameterValues(ControllerContext controllerContext, ActionDescriptor actionDescriptor)
        {
            var context = controllerContext.HttpContext;
            IActionParametersProvider provider = ActionParametersProviderFactory.CreateActionParametersProvider(context.Request);

            return(provider.GetParameters(context.Request, actionDescriptor));
        }
Example #2
0
        private static object[] GetActionCallParameters(HttpContext context, ActionDescription action)
        {
            if (action.Parameters == null || action.Parameters.Length == 0)
            {
                return(null);
            }

            IActionParametersProvider provider = ActionParametersProviderFactory.CreateActionParametersProvider(context.Request);

            return(provider.GetParameters(context.Request, action));
        }
Example #3
0
        private object[] GetActionParameters()
        {
            ActionDescription action = this.InvokeInfo.Action;

            if (action.Parameters == null || action.Parameters.Length == 0)
            {
                return(null);
            }



            IActionParametersProvider provider
                = ActionParametersProviderFactory.Instance.CreateProvider(this.HttpContext);

            object[] parameters = null;

            IActionParametersProvider2 p2 = provider as IActionParametersProvider2;

            if (p2 != null)                             // 优先使用内部接口版本
            {
                parameters = p2.GetParameters(this.HttpContext, action);
            }
            else
            {
                parameters = provider.GetParameters(this.HttpContext, action.MethodInfo);
            }


            EventHandler <GetActionParametersEventArgs> eventHandler = this.OnGetActionParameters;

            if (eventHandler != null)
            {
                GetActionParametersEventArgs e = new GetActionParametersEventArgs {
                    HttpContext  = this.HttpContext,
                    ActionMethod = this.InvokeInfo.Action.MethodInfo,
                    Parameters   = parameters
                };

                eventHandler(this, e);

                return(e.Parameters);
            }

            return(parameters);
        }
Example #4
0
        private object[] ExecuteObtainParameters()
        {
            ActionDescription action = this.InvokeInfo.Action;

            if (action.Parameters == null || action.Parameters.Length == 0)
            {
                return(null);
            }


            // 从工厂中获取一个能够构造Action参数的提供者实例。
            IActionParametersProvider provider
                = ActionParametersProviderFactory.Instance.CreateProvider(this.HttpContext);

            object[] parameters = null;

            // 优先使用内部接口版本
            IActionParametersProvider2 p2 = provider as IActionParametersProvider2;

            if (p2 != null)
            {
                parameters = p2.GetParameters(this.HttpContext, action);
            }
            else
            {
                parameters = provider.GetParameters(this.HttpContext, action.MethodInfo);
            }


            // 引发事件,允许在外部修改获取到的事件参数,可修改数组中元素,但数组本身不允许重新指定。
            EventHandler <EndObtainParametersEventArgs> eventHandler = this.EndObtainParameters;

            if (eventHandler != null)
            {
                EndObtainParametersEventArgs e = CreateEventArgs <EndObtainParametersEventArgs>();
                e.Parameters = parameters;

                // 调用外部的事件处理器
                eventHandler(this, e);
            }

            return(parameters);
        }
Example #5
0
        /// <summary>
        /// 根据HttpContext创建一个匹配的IActionParametersProvider实例
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public virtual IActionParametersProvider CreateProvider(HttpContext context)
        {
            // 扩展点:允许自定义 IActionParametersProvider从HTTP请求中解析数据

            if (context == null)
            {
                throw new ArgumentNullException("context");
            }


            string contentType = context.Request.ContentType;

            if (string.IsNullOrEmpty(contentType) ||
                contentType.IndexOfIgnoreCase(RequestContentType.Form) >= 0 ||
                contentType.IndexOfIgnoreCase(RequestContentType.Multipart) >= 0)
            {
                return(CreateFormProvider(context));
            }

            if (contentType.IndexOfIgnoreCase(RequestContentType.Json) >= 0)
            {
                return(CreateJsonProvider(context));
            }


            if (contentType.IndexOfIgnoreCase(RequestContentType.Xml) >= 0)
            {
                return(CreateXmlProvider(context));
            }


            IActionParametersProvider provider = CreateCustomizeProvider(context);

            if (provider != null)
            {
                return(provider);
            }


            // 其它未能识别的格式,都会按默认方式处理
            return(CreateDefaultProvider(context));
        }
Example #6
0
        /// <summary>
        /// 根据HttpContext创建一个匹配的IActionParametersProvider实例
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public virtual IActionParametersProvider CreateProvider(HttpContext context)
        {
            // 扩展点:允许自定义 IActionParametersProvider从HTTP请求中解析数据

            if (context == null)
            {
                throw new ArgumentNullException("context");
            }


            string contentType = context.Request.ContentType;

            if (string.IsNullOrEmpty(contentType) ||
                contentType.IndexOfIgnoreCase("application/x-www-form-urlencoded") >= 0)
            {
                return(CreateFormProvider(context));
            }

            if (contentType.IndexOfIgnoreCase("application/json") >= 0)
            {
                return(CreateJsonProvider(context));
            }


            if (contentType.IndexOfIgnoreCase("application/xml") >= 0)
            {
                return(CreateXmlProvider(context));
            }


            IActionParametersProvider provider = CreateCustomizeProvider(context);

            if (provider != null)
            {
                return(provider);
            }


            // 默认还是表单的 key = vlaue格式。
            // "multipart/form-data", "text/plain" 也会到这里
            return(CreateDefaultProvider(context));
        }