转自: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, , 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 > )
{
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 > )
{
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实例的更多相关文章

  1. asp.net中http提交数据所遇到的那些坑

    http提交数据有两种形式,get和post,不知道的同学请联系度娘. 1.aspnet:MaxHttpCollectionKeys 业务场景:业务很简单,手机端读取本地通讯录,将所有通讯录提交到后台 ...

  2. ASP.NET中POST提交数据并跳转页面

    需求:先Post提交数据,然后跳转到目标页面 找了好久才发现这个神奇的类HttpHelper.原理很简单,利用html的from表单拼接,然后执行 使用方法: NameValueCollection ...

  3. Asp.Net Mvc4 Ajax提交数据成功弹框后跳转页面

    1.cshtml页面代码 @model Model.UserInfo @{     ViewBag.Title = "Edit"; var options = new AjaxOp ...

  4. Asp.Net模拟post提交数据方法

    方法1: System.Net.WebClient WebClientObj = new System.Net.WebClient(); System.Collections.Specialized. ...

  5. 采用get方式提交数据到服务器实例

    GetDemo项目目录 一.编写StreamTools.java /** * */ package com.hyzhou.getdemo.utiils; import java.io.ByteArra ...

  6. Silverlight实例教程 - Validation用户提交数据验证捕获(转载)

    Silverlight 4 Validation验证实例系列 Silverlight实例教程 - Validation数据验证开篇 Silverlight实例教程 - Validation数据验证基础 ...

  7. <转>ASP.NET学习笔记之MVC 3 数据验证 Model Validation 详解

    MVC 3 数据验证 Model Validation 详解  再附加一些比较好的验证详解:(以下均为引用) 1.asp.net mvc3 的数据验证(一) - zhangkai2237 - 博客园 ...

  8. 小范笔记:ASP.NET Core API 基础知识与Axios前端提交数据

    跟同事合作前后端分离项目,自己对 WebApi 的很多知识不够全,虽说不必要学全栈,可是也要了解基础知识,才能合理设计接口.API,方便与前端交接. 晚上回到宿舍后,对 WebApi 的知识查漏补缺, ...

  9. ASP模拟POST请求异步提交数据的方法

    这篇文章主要介绍了ASP模拟POST请求异步提交数据的方法,本文使用MSXML2.SERVERXMLHTTP.3.0实现POST请求,需要的朋友可以参考下 有时需要获取远程网站的某些信息,而服务器又限 ...

随机推荐

  1. Codevs 1689 建造高塔

    1689 建造高塔 时间限制: 1 s 空间限制: 128000 KB 题目等级 : **钻石 Diamond** 题目描述 Description n有n种石块,石块能无限供应.每种石块都是长方体, ...

  2. C++学习指南

    转载于stackoverflow:http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list 感谢Ge ...

  3. @using (Html.BeginForm())收集

    一 ,制定表单提交方式和路径 1,指明(视图,控制器,提交方式,参数) <%using(Html.BeginForm("Index","Home",For ...

  4. [转]left join,right join,inner join区别

    left join(左联接) 返回包括左表中的所有记录和右表中联结字段相等的记录 right join(右联接) 返回包括右表中的所有记录和左表中联结字段相等的记录inner join(等值连接) 只 ...

  5. GitHub使用教程for Eclipse

    1.下载egit插件http://www.eclipse.org/egit/ http://www.eclipse.org/egit/download/ Installing the Latest R ...

  6. (转载).Net HttpPost的发送和接收示例代码

    HttpPost在不同系统进行数据交互的时候经常被使用.它的最大好处在于直接,不像Webservice或者WCF需要wsdl作为一个双方的"中介".在安全性上,往往通过IP限制的方 ...

  7. Codeforces Round #336 (Div. 1) A - Chain Reaction

    Chain Reaction 题意:有n(1 ≤ n ≤ 100 000) 个灯泡,每个灯泡有一个位置a以及向左照亮的范围b (0 <= a <= 1e6 ,1<= b <= ...

  8. pyes-elasticsearch的python客户端使用笔记

    elasticsearch入门:  http://www.qwolf.com/?p=1387 一.重要的概念 http://834945712.iteye.com/blog/1915432 这篇文章很 ...

  9. c# .net使用SqlDataReader注意的几点

    转自:http://blog.knowsky.com/258608.htm 1.当SqlDataReader没有关闭之前,数据库连接会一直保持open状态,所以在使用SqlDataReader时,使用 ...

  10. Uva 10288 Coupons

    Description Coupons in cereal boxes are numbered \(1\) to \(n\), and a set of one of each is require ...