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>//<RETURNS>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