asp.NetPost Getコミットデータ転送Modelインスタンス
17304 ワード
回転元:http://blog.csdn.net/daodaowolf/article/details/8990694
この機能は、クライアントHTTPプロトコルPOST GET方式でコミットされたデータをあるModelインスタンスに変換し、クライアントブラウザAjaxでコミットされたキー値ペアまたはjson形式のデータを直接Modelクラスに変換するインスタンスである.
くだらないことは言わないで、直接コードを貼ってください.
View Code
この機能は、クライアント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