校园春色亚洲色图_亚洲视频分类_中文字幕精品一区二区精品_麻豆一区区三区四区产品精品蜜桃

主頁 > 知識(shí)庫 > 基于.NET中:自動(dòng)將請(qǐng)求參數(shù)綁定到ASPX、ASHX和MVC的方法(菜鳥必看)

基于.NET中:自動(dòng)將請(qǐng)求參數(shù)綁定到ASPX、ASHX和MVC的方法(菜鳥必看)

熱門標(biāo)簽:五常地圖標(biāo)注 鄭州400電話辦理 聯(lián)通 萊蕪?fù)夂綦婁N機(jī)器人價(jià)格 電銷語音自動(dòng)機(jī)器人 地圖標(biāo)注和認(rèn)領(lǐng) 戶外地圖標(biāo)注軟件手機(jī)哪個(gè)好用 智能電話營銷外呼系統(tǒng) 長春呼叫中心外呼系統(tǒng)哪家好 凱立德導(dǎo)航官網(wǎng)地圖標(biāo)注

前言

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

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

自動(dòng)綁定到ASPX和ASHX

框架支持

復(fù)制代碼 代碼如下:

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

 namespace Happy.Web
 {
     public interface IWantAutoBindProperty
     {
     }
 }

復(fù)制代碼 代碼如下:

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

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

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

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

%@ 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ù)制代碼 代碼如下:

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);
         }
     }
 }

您可能感興趣的文章:
  • ashx中使用session的方法(獲取session值)
  • ASP.NET ASHX中獲得Session的方法
  • Asp.net在ashx文件中處理Session問題解決方法
  • 在ashx文件中使用session的解決思路
  • ashx介紹以及ashx文件與aspx文件之間的區(qū)別
  • ashx文件的使用小結(jié)
  • aspx與ascx,ashx的用法總結(jié)
  • 后綴為 ashx 與 axd 的文件區(qū)別淺析
  • *.ashx文件不能訪問Session值的解決方法

標(biāo)簽:紅河 福州 西寧 岳陽 衢州 宣城 西藏 湖州

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《基于.NET中:自動(dòng)將請(qǐng)求參數(shù)綁定到ASPX、ASHX和MVC的方法(菜鳥必看)》,本文關(guān)鍵詞  基于,.NET,中,自動(dòng),將,請(qǐng)求,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《基于.NET中:自動(dòng)將請(qǐng)求參數(shù)綁定到ASPX、ASHX和MVC的方法(菜鳥必看)》相關(guān)的同類信息!
  • 本頁收集關(guān)于基于.NET中:自動(dòng)將請(qǐng)求參數(shù)綁定到ASPX、ASHX和MVC的方法(菜鳥必看)的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    主站蜘蛛池模板: 盐津县| 闵行区| 右玉县| 胶南市| 西乌| 承德市| 昌宁县| 尖扎县| 彭山县| 石景山区| 项城市| 临朐县| 贵州省| 托里县| 大方县| 黄大仙区| 潼关县| 定兴县| 麦盖提县| 海阳市| 沙坪坝区| 仪陇县| 兴仁县| 小金县| 新巴尔虎左旗| 舟曲县| 秦皇岛市| 白河县| 墨竹工卡县| 高邮市| 灌阳县| 东兴市| 大名县| 屏山县| 马山县| 高雄县| 九龙坡区| 剑阁县| 通江县| 盐城市| 都昌县|