Request.From,Request.QueryString转对象

Posted plming

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Request.From,Request.QueryString转对象相关的知识,希望对你有一定的参考价值。

From表单转化为对象

技术分享图片
        public static T RequestFormEntities<T>(HttpRequestBase request) where T : new()
        {
            T entity = Activator.CreateInstance<T>();
            PropertyInfo[] attrs = entity.GetType().GetProperties();
            foreach (PropertyInfo p in attrs)
            {
                foreach (string key in request.Form.AllKeys)
                {
                    if (string.Compare(p.Name, key, true) == 0)
                    {
                        try
                        {
                            Type type = p.PropertyType;
                            //判断type类型是否为泛型,因为nullable是泛型类
                            if (type.IsGenericType && type.GetGenericTypeDefinition().Equals(typeof(Nullable<>)))//判断convertsionType是否为nullable泛型类  
                            {
                                //如果type为nullable类,声明一个NullableConverter类,该类提供从Nullable类到基础基元类型的转换  
                                System.ComponentModel.NullableConverter nullableConverter = new System.ComponentModel.NullableConverter(type);
                                //将type转换为nullable对的基础基元类型  
                                type = nullableConverter.UnderlyingType;
                            }

                            p.SetValue(entity, Convert.ChangeType(request.Form[key], type), null);
                        }
                        catch (Exception e)
                        {
                            continue;
                        }
                    }
                }
            }
            return entity;
        }
View Code

Request参数转化为对象

技术分享图片
         public static T RequestStringFormEntities<T>(string request) where T : new()
        {
            T entity = Activator.CreateInstance<T>();
            PropertyInfo[] attrs = entity.GetType().GetProperties();
            foreach (PropertyInfo p in attrs)
            {
                foreach (string key in request.Split(&))
                {
                    var _key = key.Split(=)[0];
                    var _value = key.Split(=)[1];
                    if (string.Compare(p.Name, _key, true) == 0)
                    {
                        try
                        {
                            Type type = p.PropertyType;
                            //判断type类型是否为泛型,因为nullable是泛型类
                            if (type.IsGenericType && type.GetGenericTypeDefinition().Equals(typeof(Nullable<>)))//判断convertsionType是否为nullable泛型类  
                            {
                                //如果type为nullable类,声明一个NullableConverter类,该类提供从Nullable类到基础基元类型的转换  
                                System.ComponentModel.NullableConverter nullableConverter = new System.ComponentModel.NullableConverter(type);
                                //将type转换为nullable对的基础基元类型  
                                type = nullableConverter.UnderlyingType;
                            }

                            p.SetValue(entity, Convert.ChangeType(_value, type), null);
                        }
                        catch (Exception e)
                        {
                            continue;
                        }
                    }
                }
            }
            return entity;
        }
View Code

 

以上是关于Request.From,Request.QueryString转对象的主要内容,如果未能解决你的问题,请参考以下文章

asmx 接受 ajax post,jQuery ajax request from asmx web service

How to resolve the 403 error when send POST request from Postman

OGG 12.1: WARNING OGG-00936 Access denied (request from xxxx, rule #0)

如何配置 Spring Security 以避免 405 Method Not Allowed for PUT request from Angular?

ImportError:无法从“flask”导入名称“request”

asp中request.form用法