ASP. NET Post get submit data to model instance

Source: Internet
Author: User

This function is to convert the client HTTP protocol post get data submitted to a model instance, for the client browser Ajax committed key value pairs or JSON format data directly into the model class instance;

/********************************************************************************** Tyler** Founding Time: 2013-05-28** Description: Get,post data submitted by JS Ajax or HTTP is converted to the specified model instance **************************************************************** /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 submission JSON format converted to entity class///</summary>//<typeparam name= "T" > Type </typeparam>  <param name= "Myrequest" >request objects </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 excep tion ("Serialize Error:" + ex.)                    Message);        }} else throw new Exception ("Not KeyValue"); }///<summary>///Post submit Form collection convert to entity class///</summary>//<typeparam name="T" > Type </typeparam>//<param name= "myrequest" >request objects </param>//&LT;RETURNS&GT;T&L t;/returns> public static T formtomodelhttppost<t> (HttpRequest myrequest) {DataContract            Jsonserializer 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 commit JSON format converted to entity class///</summary>//<typeparam name= "T" &G t; type </typeparam>//<param name= "myrequest" >request objects </param>//<returns>t</re turns> public static T stramtomodelhttpget<t> (String queryString) {string jsonstr = que            rystring; 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 submitted QueryString collection converted to entity class////</summary>//<typeparam Nam E= "T" > Type </typeparam>//<param name= "myrequest" >request objects </param>//<returns>t </returns> public static T formtomodelhttpget<t> (HttpRequest myrequest) {Datacontrac            Tjsonserializer 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 submit data to model instance

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.