溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

.NET中如何自動(dòng)將請(qǐng)求參數(shù)綁定到ASPX、ASHX和MVC

發(fā)布時(shí)間:2021-10-13 10:31:03 來(lái)源:億速云 閱讀:168 作者:小新 欄目:開發(fā)技術(shù)

這篇文章主要介紹了.NET中如何自動(dòng)將請(qǐng)求參數(shù)綁定到ASPX、ASHX和MVC,具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。

前言

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


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

自動(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);
         }
     }
 }

感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“.NET中如何自動(dòng)將請(qǐng)求參數(shù)綁定到ASPX、ASHX和MVC”這篇文章對(duì)大家有幫助,同時(shí)也希望大家多多支持億速云,關(guān)注億速云行業(yè)資訊頻道,更多相關(guān)知識(shí)等著你來(lái)學(xué)習(xí)!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI