国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

基于.NET中:自動將請求參數(shù)綁定到ASPX、ASHX和MVC的方法(菜鳥必看)_.Net教程

2024-07-10 12:52:28
字體:
供稿:網(wǎng)友

推薦:水晶易表調(diào)用C#的WebService,返回?cái)?shù)據(jù)集合的應(yīng)用分析
本篇文章介紹了,水晶易表調(diào)用C#的WebService,返回?cái)?shù)據(jù)集合的應(yīng)用分析。需要的朋友參考下

前言

剛開始做AJAX應(yīng)用的時(shí)候,經(jīng)常要手工解析客戶端傳遞的參數(shù),這個(gè)過程極其無聊,而且代碼中充斥著:Request["xxx"]之類的代碼。

這篇文章的目的就是告訴初學(xué)者如何自動將客戶端用AJAX發(fā)送的參數(shù)自動綁定為強(qiáng)類型的成員屬性或方法參數(shù)。

自動綁定到ASPX和ASHX

框架支持

復(fù)制代碼 代碼如下:www.CuoXIn.com

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Happy.Web
{
public interface IWantAutoBindProperty
{
}
}

復(fù)制代碼 代碼如下:www.CuoXIn.com

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ù)制代碼 代碼如下:www.CuoXIn.com

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ù)制代碼 代碼如下:www.CuoXIn.com

<?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ù)制代碼 代碼如下:www.CuoXIn.com

/// <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ù)制代碼 代碼如下:www.CuoXIn.com

<%@ 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é)果

 

自動綁定到MVC
框架支持

復(fù)制代碼 代碼如下:www.CuoXIn.com

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í)例的兩種模式的使用方法。需要的朋友參考下

發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 乌拉特后旗| 菏泽市| 井冈山市| 弋阳县| 永兴县| 青浦区| 海原县| 集安市| 东乡族自治县| 通海县| 社旗县| 红桥区| 贵州省| 崇仁县| 临潭县| 凉城县| 团风县| 庆城县| 合川市| 淮南市| 伊金霍洛旗| 乐昌市| 德化县| 博野县| 囊谦县| 胶南市| 汽车| 福鼎市| 喀喇| 璧山县| 峨边| 元江| 共和县| 曲水县| 许昌市| 视频| 宣城市| 和田市| 金寨县| 蓬莱市| 东阿县|