zoukankan      html  css  js  c++  java
  • asp.net Post Get提交数据转Model实例

    转自:http://blog.csdn.net/daodaowolf/article/details/8990694

    此功能是将客户端HTTP协议POST GET方式提交的数据转换为某个Model实例,对于客户端浏览器Ajax提交的键值对或json格式数据直接转换为Model类的实例;

    废话不多说,直接贴代码。

    /********************************************************************************
    
    ** 作者:Tyler
    
    ** 创始时间:2013-05-28
    
    ** 描述:通过js ajax 或 HTTP其他方式提交的GET,POST数据转换为指定的Model实例
    
    *********************************************************************************/
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Runtime.Serialization.Json;
    using System.Web.Script.Serialization;
    using System.IO;
    using System.Text;
    using System.Collections.Specialized;
    
    namespace MyHttpRequest
    {
        public class RequestDataToCls
        {
            /// <summary>
            /// Post提交JSON格式转换为实体类
            /// </summary>
            /// <typeparam name="T">类型</typeparam>
            /// <param name="myrequest">Request对象</param>
            /// <returns>T</returns>
            public static T StramTomodelHttpPost<T>(HttpRequest myrequest)
            {
                byte[] byts = new byte[myrequest.InputStream.Length];
                myrequest.InputStream.Read(byts, 0, byts.Length);
                string jsonstr = System.Text.Encoding.Default.GetString(byts);
                if (!String.IsNullOrEmpty(jsonstr))
                {
                    DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T));
                        JavaScriptSerializer jss = new JavaScriptSerializer();
                        try
                        {
                            using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(jsonstr)))
                            {
                                T jsonObject = (T)ser.ReadObject(ms);
                                return jsonObject;
                            }
                        }
                        catch (Exception ex)
                        {
                            throw new Exception("Serialize Error: " + ex.Message);
                        }
                    }
                    else
                        throw new Exception("Not KeyValue ");
            }
    
            /// <summary>
            ///  Post提交Form集合转换为实体类
            /// </summary>
            /// <typeparam name="T">类型</typeparam>
            /// <param name="myrequest">Request对象</param>
            /// <returns>T</returns>
            public static T FormTomodelHttpPost<T>(HttpRequest myrequest)
            {
    
                DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T));
                NameValueCollection coll = myrequest.Form as NameValueCollection;
                IDictionary<string, object> idc = new Dictionary<string, object>();
                foreach (string name in coll.Keys)
                {
                    idc.Add(name, coll[name].ToString());
                }
                if (idc.Count > 0)
                {
                    JavaScriptSerializer jss = new JavaScriptSerializer();
                    string jsonstr;
                    try
                    {
                        jsonstr = jss.Serialize(idc);
                        using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(jsonstr)))
                        {
                            T jsonObject = (T)ser.ReadObject(ms);
                            return jsonObject;
                        }
                    }
                    catch (Exception ex)
                    {
                        throw new Exception("Serialize Error: " + ex.Message);
                    }
                }
                else
                    throw new Exception("Not KeyValue ");
            }
    
     
    
            /// <summary>
            /// Get提交JSON格式转换为实体类
            /// </summary>
            /// <typeparam name="T">类型</typeparam>
            /// <param name="myrequest">Request对象</param>
            /// <returns>T</returns>
            public static T StramTomodelHttpGet<T>(string queryString)
            {
                string jsonstr = queryString;
                if (!String.IsNullOrEmpty(jsonstr))
                {
                    DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T));
                    JavaScriptSerializer jss = new JavaScriptSerializer();
                    try
                    {
                        using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(jsonstr)))
                        {
                            T jsonObject = (T)ser.ReadObject(ms);
                            return jsonObject;
                        }
                    }
                    catch (Exception ex)
                    {
                        throw new Exception("Serialize Error: " + ex.Message);
                    }
                }
                else
                    throw new Exception("Not KeyValue ");
            }
    
            /// <summary>
            ///  Get提交QueryString集合转换为实体类
            /// </summary>
            /// <typeparam name="T">类型</typeparam>
            /// <param name="myrequest">Request对象</param>
            /// <returns>T</returns>
            public static T FormTomodelHttpGet<T>(HttpRequest myrequest)
            {
                DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T));
                NameValueCollection coll = myrequest.QueryString as NameValueCollection;
                IDictionary<string, object> idc = new Dictionary<string, object>();
                foreach (string name in coll.Keys)
                {
                    idc.Add(name, coll[name].ToString());
                }
                if (idc.Count > 0)
                {
                    JavaScriptSerializer jss = new JavaScriptSerializer();
                    string jsonstr;
                    try
                    {
                        jsonstr = jss.Serialize(idc);
                        using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(jsonstr)))
                        {
                            T jsonObject = (T)ser.ReadObject(ms);
                            return jsonObject;
                        }
                    }
                    catch (Exception ex)
                    {
                        throw new Exception("Serialize Error: " + ex.Message);
                    }
                }
                else
                    throw new Exception("Not KeyValue ");
            }
        }
    }
    View Code
  • 相关阅读:
    jboss服务器下的中文乱码问题
    rpm数据库被损坏修复方案
    python http post简单例子
    python 获取时间戳相关计算
    Qt 之Excel 操作(二 强化版本)
    SqlServer查询某重复列根据条件取一条数据
    查看各表所占空间
    Taro 1.3.x版本 编译时报错 UnhandledPromiseRejectionWarning: Error: ENOENT: no such file or directory, open 'util'
    React index.html引入script时 src中的斜杠都变成了空格,并且还多出了script标签 导致无法加载
    支付宝小程序 iOS报页面访问受限aboud:srcdoc android无此问题 2021记录
  • 原文地址:https://www.cnblogs.com/cugwx/p/3615754.html
Copyright © 2011-2022 走看看