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;
            }
        }
    }
  • 相关阅读:
    四月十五号日报
    四月十一号日报
    四月八号日报
    五月六号日报
    CCSUOJ评测系统——第四次scrum冲刺
    CCSUOJ评测系统——第三次scrum冲刺
    CCSUOJ评测系统——第二次scrum冲刺
    C# Process 进程管理
    [C#][收集整理]
    [C#][收集整理]
  • 原文地址:https://www.cnblogs.com/raorao1994/p/10188898.html
Copyright © 2011-2022 走看看