当前位置: 首页>>代码示例>>C#>>正文


C# HttpRequest.GetPostJson方法代码示例

本文整理汇总了C#中HttpRequest.GetPostJson方法的典型用法代码示例。如果您正苦于以下问题:C# HttpRequest.GetPostJson方法的具体用法?C# HttpRequest.GetPostJson怎么用?C# HttpRequest.GetPostJson使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在HttpRequest的用法示例。


在下文中一共展示了HttpRequest.GetPostJson方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Invoke

        /// <summary>
        /// Invokes the specified method information.
        /// <remarks>
        /// Invoke action would regard to method parameter to use difference logic. Following steps show the IF-ELSE case. When it is hit, other would not go through.
        /// <list type="number"><item>
        /// If input parameter count is 0, invoke without parameter object.
        /// </item><item>
        /// If input parameter count is 1 and key is not empty or null, invoke using key.
        /// </item><item>
        /// If input parameter count is 1 and key is empty or null, invoke using key, try to get JSON object from request body and convert to object for invoke.
        /// </item><item>
        /// If input parameter count more than 1, try read JSON data to match parameters by name (ignore case) in root level, then invoke.
        /// </item></list></remarks></summary>
        /// <param name="instance">The instance.</param>
        /// <param name="methodInfo">The method information.</param>
        /// <param name="httpRequest">The HTTP request.</param>
        /// <param name="key">The key.</param>
        /// <param name="jsonBody">The json body.</param>
        /// <returns>System.Object.</returns>
        protected virtual object Invoke(object instance, MethodInfo methodInfo, HttpRequest httpRequest, string key, out string jsonBody)
        {
            var inputParameters = methodInfo.GetParameters();
            jsonBody = null;

            if (!string.IsNullOrWhiteSpace(key) && key.Contains('%'))
            {
                key = key.ToUrlDecodedText();
            }

            if (inputParameters.Length == 0)
            {
                return methodInfo.Invoke(instance, null);
            }
            else if (inputParameters.Length == 1)
            {
                if (!string.IsNullOrWhiteSpace(key) && (inputParameters[0].ParameterType == typeof(string) || inputParameters[0].ParameterType.IsValueType))
                {
                    return methodInfo.Invoke(instance, new object[] { ReflectionExtension.ConvertToObjectByType(inputParameters[0].ParameterType, key) });
                }
                else
                {
                    var json = jsonBody = httpRequest.GetPostJson(Encoding.UTF8);
                    return methodInfo.Invoke(instance, new object[] { DeserializeJsonObject(json, inputParameters[0].ParameterType) });
                }
            }
            else
            {
                object[] parameters = new object[inputParameters.Length];

                if (httpRequest.QueryString.Count > 0)
                {
                    var start = 1;
                    if (!string.IsNullOrWhiteSpace(key) && (inputParameters[0].ParameterType.TryGetNullableType().IsValueType))
                    {
                        parameters[0] = ReflectionExtension.ConvertToObjectByType(inputParameters[0].ParameterType, key);
                    }
                    else
                    {
                        var json = jsonBody = httpRequest.GetPostJson(Encoding.UTF8);
                        parameters[0] = json.TryConvertJsonToObject();
                        if (parameters[0] == null)
                        {
                            start = 0;
                        }
                    }

                    for (var i = start; i < inputParameters.Length; i++)
                    {
                        parameters[i] = ReflectionExtension.ConvertToObjectByType(inputParameters[i].ParameterType,
                            httpRequest.QueryString.Get(inputParameters[i].Name));
                    }
                }
                else
                {
                    var json = jsonBody = httpRequest.GetPostJson(Encoding.UTF8);
                    var jsonObject = string.IsNullOrWhiteSpace(json) ? null : JObject.Parse(json);

                    if (jsonObject != null)
                    {
                        for (int i = 0; i < inputParameters.Length; i++)
                        {
                            var jTokenObject = jsonObject.GetProperty(inputParameters[i].Name);
                            parameters[i] = jTokenObject == null ? null : jTokenObject.ToObject(inputParameters[i].ParameterType);
                        }
                    }
                }

                return methodInfo.Invoke(instance, parameters);
            }
        }
开发者ID:rynnwang,项目名称:CommonSolution,代码行数:90,代码来源:ApiHandlerBase.cs


注:本文中的HttpRequest.GetPostJson方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。