zoukankan      html  css  js  c++  java
  • HTTP上下文表单内容转为实体对象

    using ServiceStack.Web;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Reflection;
    using System.Text;
    using System.Threading.Tasks;
    using System.Web;
    
    namespace restService.Interface.Helper
    {
        public static class EntityHelper
        {
            /// <summary>
            /// 将HTTP上下文表单内容转为实体对象
            /// </summary>
            /// <typeparam name="T"></typeparam>
            /// <param name="context"></param>
            /// <returns></returns>
            public static T RequestToModel<T>(this HttpContext context) where T : new()
            {
                T model = new T();
                Type type = model.GetType();
                PropertyInfo[] Fields = type.GetProperties();
                for (int i = 0; i < Fields.Count(); i++)
                {
                    var name = Fields[i].Name;
                    Type vtype = Fields[i].PropertyType;
                    var value = context.Request[name];
                    if (value != null)
                    {
                        if (vtype.Name.ToLower().IndexOf("int") >= 0)//int
                        {
                            var v = Convert.ToInt32(value);
                            Fields[i].SetValue(model, v);
                            continue;
                        }
                        if (vtype.Name.ToLower().IndexOf("string") >= 0)//string
                        {
                            var v = Convert.ToString(value);
                            Fields[i].SetValue(model, v);
                            continue;
                        }
                        if (vtype.Name.ToLower().IndexOf("datetime") >= 0)//datetime
                        {
                            var v = Convert.ToDateTime(value);
                            Fields[i].SetValue(model, v);
                        }
                        if (vtype.Name.ToLower().IndexOf("bool") >= 0)//datetime
                        {
                            var v = Convert.ToBoolean(value);
                            Fields[i].SetValue(model, v);
                        }
                    }
                }
                return model;
            }
            /// <summary>
            /// 将HTTP上下文表单内容转为实体对象
            /// </summary>
            /// <typeparam name="T"></typeparam>
            /// <param name="context"></param>
            /// <returns></returns>
            public static T RequestToModel<T>(this IRequest context) where T : new()
            {
                T model = new T();
                Type type = model.GetType();
                PropertyInfo[] Fields = type.GetProperties();
                for (int i = 0; i < Fields.Count(); i++)
                {
                    var name = Fields[i].Name;
                    Type vtype = Fields[i].PropertyType;
                    string value = context.QueryString[name];
                    if(value==null)
                        value = context.FormData[name];
                    if (value != null)
                    {
                        if (vtype.Name.ToLower().IndexOf("int") >= 0)//int Nullable
                        {
                            var v = Convert.ToInt32(value);
                            Fields[i].SetValue(model, v);
                            continue;
                        }
                        if (vtype.Name.ToLower().IndexOf("nullable") >= 0)//Nullable
                        {
                            var v = Convert.ToDecimal(value);
                            Fields[i].SetValue(model, v);
                            continue;
                        }
                        if (vtype.Name.ToLower().IndexOf("string") >= 0)//string
                        {
                            var v = Convert.ToString(value);
                            Fields[i].SetValue(model, v);
                            continue;
                        }
                        if (vtype.Name.ToLower().IndexOf("datetime") >= 0)//datetime
                        {
                            var v = Convert.ToDateTime(value);
                            Fields[i].SetValue(model, v);
                        }
                        if (vtype.Name.ToLower().IndexOf("bool") >= 0)//datetime
                        {
                            var v = Convert.ToBoolean(value);
                            Fields[i].SetValue(model, v);
                        }
                    }
                }
                return model;
            }
            /// <summary>
            /// 获取数据
            /// </summary>
            /// <param name="context"></param>
            /// <param name="key">关键字</param>
            /// <returns></returns>
            public static String GetDataParameter(this IRequest context,string key)
            {
                string result = context.FormData[key];
                if (result == null)
                    result = context.QueryString[key];
                return result;
            }
        }
    }
  • 相关阅读:
    Sharepoint 2013默认dll部署位置
    Sharepoint 2010 Form认证自定义登录页面,总是自动登录问题
    cocos打包后页面在ios浏览器、安卓钉钉等部分app中横竖屏问题
    cocos构建出来的 Web Mobile 项目在微信开发者工具里面无法点击
    git 合并某个提交commit到指定的分支上
    cc.sys.localStorage存储和读取用户数据
    cocos creater关于blend,关于预乘premultiply alpha,关于图片白边灰边的问题
    cocos遮罩层点击穿透问题解决
    postman中 form-data、x-www-form-urlencoded、raw、binary操作
    ‘webpack-dev-server' 不是内部或外部命令,也不是可运行的程序
  • 原文地址:https://www.cnblogs.com/raorao1994/p/10188898.html
Copyright © 2011-2022 走看看