基于.NET中:自動(dòng)將請(qǐng)求參數(shù)綁定到ASPX、ASHX和MVC的方法(菜鳥(niǎo)必看)_.Net教程
推薦:水晶易表調(diào)用C#的WebService,返回?cái)?shù)據(jù)集合的應(yīng)用分析本篇文章介紹了,水晶易表調(diào)用C#的WebService,返回?cái)?shù)據(jù)集合的應(yīng)用分析。需要的朋友參考下
前言
剛開(kāi)始做AJAX應(yīng)用的時(shí)候,經(jīng)常要手工解析客戶端傳遞的參數(shù),這個(gè)過(guò)程極其無(wú)聊,而且代碼中充斥著:Request["xxx"]之類的代碼。
這篇文章的目的就是告訴初學(xué)者如何自動(dòng)將客戶端用AJAX發(fā)送的參數(shù)自動(dòng)綁定為強(qiáng)類型的成員屬性或方法參數(shù)。
自動(dòng)綁定到ASPX和ASHX
框架支持
復(fù)制代碼 代碼如下:hl5o.cn
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Happy.Web
{
public interface IWantAutoBindProperty
{
}
}
復(fù)制代碼 代碼如下:hl5o.cn
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Happy.Web
{
[AttributeUsage(AttributeTargets.Property, AllowMultiple = true)]
public sealed class AutoBind : Attribute
{
}
}
復(fù)制代碼 代碼如下:hl5o.cn
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using Newtonsoft.Json;
using Happy.ExtensionMethods.Reflection;
namespace Happy.Web
{
public class JsonBinderModule : IHttpModule
{
public void Init(HttpApplication context)
{
context.PreRequestHandlerExecute += OnPreRequestHandlerExecute;
}
private void OnPreRequestHandlerExecute(object sender, EventArgs e)
{
if (!(HttpContext.Current.CurrentHandler is IWantAutoBindProperty))
{
return;
}
var properties = HttpContext.Current.CurrentHandler.GetType().GetProperties();
foreach (var property in properties)
{
if (!property.IsDefined(typeof(AutoBind), true))
{
continue;
}
string json = HttpContext.Current.Request[property.Name];
var value = JsonConvert.DeserializeObject(json, property.PropertyType);
property.SetValue(HttpContext.Current.Handler, value);
}
}
public void Dispose()
{
}
}
}
代碼示例
復(fù)制代碼 代碼如下:hl5o.cn
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.web>
<compilation debug="false" targetFramework="4.0" />
<httpModules>
<add name="JsonBinderModule" type="Happy.Web.JsonBinderModule"/>
</httpModules>
</system.web>
</configuration>
復(fù)制代碼 代碼如下:hl5o.cn
/// <reference path="../ext-all-debug-w-comments.js" />
var data = {
Name: '段光偉',
Age: 28
};
Ext.Ajax.request({
url: '../handlers/JsonBinderTest.ashx',
method: 'POST',
params: { user: Ext.encode(data) }
});
復(fù)制代碼 代碼如下:hl5o.cn
<%@ WebHandler Language="C#" Class="JsonBinderTest" %>
using System;
using System.Web;
using Happy.Web;
public class JsonBinderTest : IHttpHandler, IWantAutoBindProperty
{
[AutoBind]
public User user { get; set; }
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
context.Response.Write(string.Format("姓名:{0},年齡:{1}", user.Name, user.Age));
}
public bool IsReusable
{
get
{
return false;
}
}
}
public class User
{
public string Name { get; set; }
public int Age { get; set; }
}
運(yùn)行結(jié)果

自動(dòng)綁定到MVC
框架支持
復(fù)制代碼 代碼如下:hl5o.cn
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Mvc;
using Newtonsoft.Json;
namespace Tenoner.Web.Mvc
{
public class JsonBinder : IModelBinder
{
public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
string json = controllerContext.HttpContext.Request[bindingContext.ModelName];
return JsonConvert.DeserializeObject(json, bindingContext.ModelType);
}
}
}
分享:關(guān)于DDD:管理工作單元實(shí)例的兩種模式的使用方法本篇文章介紹了,關(guān)于DDD:管理工作單元實(shí)例的兩種模式的使用方法。需要的朋友參考下
相關(guān).Net教程:
- asp.net如何得到GRIDVIEW中某行某列值的方法
- .net SMTP發(fā)送Email實(shí)例(可帶附件)
- js實(shí)現(xiàn)廣告漂浮效果的小例子
- asp.net Repeater 數(shù)據(jù)綁定的具體實(shí)現(xiàn)
- Asp.Net 無(wú)刷新文件上傳并顯示進(jìn)度條的實(shí)現(xiàn)方法及思路
- Asp.net獲取客戶端IP常見(jiàn)代碼存在的偽造IP問(wèn)題探討
- VS2010 水晶報(bào)表的使用方法
- ASP.NET中操作SQL數(shù)據(jù)庫(kù)(連接字符串的配置及獲取)
- asp.net頁(yè)面?zhèn)髦禍y(cè)試實(shí)例代碼
- DataGridView - DataGridViewCheckBoxCell的使用介紹
- asp.net中javascript的引用(直接引入和間接引入)
- 三層+存儲(chǔ)過(guò)程實(shí)現(xiàn)分頁(yè)示例代碼
.Net教程Rss訂閱編程教程搜索
.Net教程推薦
- 在ASP.NET中自動(dòng)給URL加上超級(jí)鏈接
- ASP.NET的高級(jí)調(diào)試技巧
- 解讀訪問(wèn)本機(jī)IIS站點(diǎn)被要求輸入用戶名和密碼
- ASP.NET立即上手教程(4)
- 淺談ASP.NET中使用AJAX的簡(jiǎn)單方法
- 淺談ASP.NET中顯示Linq To SQL輸出的SQL語(yǔ)句
- 怎樣使ASP.NET從字符串中查找字符出現(xiàn)的次數(shù)
- 解讀VS2008中查看.NET源碼的設(shè)置方法
- ASP.NET生成靜態(tài)HTML頁(yè)面并分別按年月目錄存放
- GIS開(kāi)發(fā)隨筆--GIS技術(shù)的一點(diǎn)理解和MapNet控件試驗(yàn)
- 相關(guān)鏈接:
復(fù)制本頁(yè)鏈接| 搜索基于.NET中:自動(dòng)將請(qǐng)求參數(shù)綁定到ASPX、ASHX和MVC的方法(菜鳥(niǎo)必看)
- 教程說(shuō)明:
.Net教程-基于.NET中:自動(dòng)將請(qǐng)求參數(shù)綁定到ASPX、ASHX和MVC的方法(菜鳥(niǎo)必看)
。