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;
            }
        }
    }
  • 相关阅读:
    安装VMWare tools 及安装后/mnt中有hgfs但没共享文件的解决办法
    linux挂载命令
    RHEL7/CentOS7 Network Service开机无法启动的解决方法
    linux消息队列编程实例
    Linux进程间通信——使用消息队列
    消息队列函数
    ipcs查看消息队列命令
    linux批量删除
    HTTP 请求消息头部实例:
    drf 序列化组件
  • 原文地址:https://www.cnblogs.com/raorao1994/p/10188898.html
Copyright © 2011-2022 走看看