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

首頁 > 編程 > .NET > 正文

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

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

前言

剛開始做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

框架支持
代碼如下:
using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
 using System.Threading.Tasks;

 namespace Happy.Web
 {
     public interface IWantAutoBindProperty
     {
     }
 }

代碼如下:
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
     {
     }
 }

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

發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 黑山县| 郎溪县| 龙胜| 平湖市| 铅山县| 荥阳市| 东乡县| 孝昌县| 依兰县| 辛集市| 东宁县| 永修县| 蒙城县| 岳阳县| 涟源市| 崇阳县| 保亭| 临沧市| 神木县| 旬邑县| 嘉定区| 金湖县| 大竹县| 建昌县| 蓬安县| 门头沟区| 汉源县| 景洪市| 拉萨市| 措勤县| 凤翔县| 丹凤县| 广东省| 新泰市| 宜章县| 裕民县| 蒙自县| 神池县| 泌阳县| 都匀市| 瑞丽市|