asp.net Post Get提交資料轉Model執行個體

來源:互聯網
上載者:User

標籤:blog   http   io   ar   os   sp   for   java   on   

此功能是將用戶端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 ");        }    }}

  

asp.net Post Get提交資料轉Model執行個體

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.