Example #1
0
 /// <summary>
 /// Get all parameters into a given type
 /// </summary>
 /// <typeparam name="T">The type contains parameters</typeparam>
 /// <param name="request">Http request</param>
 /// <returns></returns>
 public static T GetAllAs <T>(this IHttpRequest request)
 {
     if (request.ContentType?.StartsWith("application/json") ?? false)
     {
         // Deserialize with json
         // It should only read once
         var json = (string)request.HttpContext.Items.GetOrCreate(
             "__json_body", () => new StreamReader(request.Body).ReadToEnd());
         return(JsonConvert.DeserializeObject <T>(json));
     }
     else if (typeof(T) == typeof(IDictionary <string, object>) ||
              typeof(T) == typeof(Dictionary <string, object>))
     {
         // Return all parameters
         return((T)(object)request.GetAllDictionary().ToDictionary(
                    p => p.Key, p => (object)p.Value.FirstOrDefault()));
     }
     else if (typeof(T) == typeof(IDictionary <string, string>) ||
              typeof(T) == typeof(Dictionary <string, string>))
     {
         // Return all parameters
         return((T)(object)request.GetAllDictionary().ToDictionary(
                    p => p.Key, p => (string)p.Value.FirstOrDefault()));
     }
     else
     {
         // Get each property by it's name
         var value = (T)Activator.CreateInstance(typeof(T));
         foreach (var property in typeof(T).FastGetProperties())
         {
             if (!property.CanRead || !property.CanWrite)
             {
                 continue;                         // Property is read or write only
             }
             object propertyValue;
             if (property.PropertyType == typeof(IHttpPostedFile))
             {
                 propertyValue = request.GetPostedFile(property.Name);
             }
             else
             {
                 propertyValue = request.Get <string>(property.Name)
                                 .ConvertOrDefault(property.PropertyType, null);
             }
             if (propertyValue != null)
             {
                 property.FastSetValue(value, propertyValue);
             }
         }
         return(value);
     }
 }
Example #2
0
        /// <summary>
        /// Get all parameters into a given type<br/>
        /// 获取Http请求中的所有参数, 以指定类型返回<br/>
        /// </summary>
        /// <typeparam name="T">The type contains parameters</typeparam>
        /// <param name="request">Http request</param>
        /// <returns></returns>
        /// <example>
        /// <code language="cs">
        /// var request = HttpManager.CurrentContext.Request;
        /// var result = request.GetAllAs&lt;TestData&gt;();
        /// </code>
        /// </example>
        public static T GetAllAs <T>(this IHttpRequest request)
        {
            var jsonBody = request.GetJsonBody();

            if (!string.IsNullOrEmpty(jsonBody))
            {
                // Deserialize with json
                return(JsonConvert.DeserializeObject <T>(jsonBody));
            }
            else if (typeof(T) == typeof(IDictionary <string, object>) ||
                     typeof(T) == typeof(Dictionary <string, object>))
            {
                // Return all parameters
                return((T)(object)request.GetAllDictionary().ToDictionary(
                           p => p.Key, p => (object)p.Value.FirstOrDefault()));
            }
            else if (typeof(T) == typeof(IDictionary <string, string>) ||
                     typeof(T) == typeof(Dictionary <string, string>))
            {
                // Return all parameters
                return((T)(object)request.GetAllDictionary().ToDictionary(
                           p => p.Key, p => (string)p.Value.FirstOrDefault()));
            }
            else
            {
                // Get each property by it's name
                var value = (T)Activator.CreateInstance(typeof(T));
                foreach (var property in typeof(T).FastGetProperties())
                {
                    if (!property.CanRead || !property.CanWrite)
                    {
                        continue;                         // Property is read or write only
                    }
                    var propertyValue = request.Get <object>(property.Name)
                                        .ConvertOrDefault(property.PropertyType, null);
                    if (propertyValue != null)
                    {
                        property.FastSetValue(value, propertyValue);
                    }
                }
                return(value);
            }
        }