好湿?好紧?好多水好爽自慰,久久久噜久噜久久综合,成人做爰A片免费看黄冈,机机对机机30分钟无遮挡

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

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

熱門標簽:五常地圖標注 鄭州400電話辦理 聯通 萊蕪外呼電銷機器人價格 電銷語音自動機器人 地圖標注和認領 戶外地圖標注軟件手機哪個好用 智能電話營銷外呼系統 長春呼叫中心外呼系統哪家好 凱立德導航官網地圖標注

前言

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

這篇文章的目的就是告訴初學者如何自動將客戶端用AJAX發送的參數自動綁定為強類型的成員屬性或方法參數。

自動綁定到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))
                 {
                     continue;
                 }

                 string json = HttpContext.Current.Request[property.Name];

                 var value = JsonConvert.DeserializeObject(json, property.PropertyType);

                 property.SetValue(HttpContext.Current.Handler, value);
             }
         }

         public void Dispose()
         {
         }
     }
 }

代碼示例
復制代碼 代碼如下:

?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>

復制代碼 代碼如下:

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

復制代碼 代碼如下:

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

運行結果

自動綁定到MVC
框架支持

復制代碼 代碼如下:

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文件之間的區別
  • ashx文件的使用小結
  • aspx與ascx,ashx的用法總結
  • 后綴為 ashx 與 axd 的文件區別淺析
  • *.ashx文件不能訪問Session值的解決方法

標簽:紅河 福州 西寧 岳陽 衢州 宣城 西藏 湖州

巨人網絡通訊聲明:本文標題《基于.NET中:自動將請求參數綁定到ASPX、ASHX和MVC的方法(菜鳥必看)》,本文關鍵詞  基于,.NET,中,自動,將,請求,;如發現本文內容存在版權問題,煩請提供相關信息告之我們,我們將及時溝通與處理。本站內容系統采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《基于.NET中:自動將請求參數綁定到ASPX、ASHX和MVC的方法(菜鳥必看)》相關的同類信息!
  • 本頁收集關于基于.NET中:自動將請求參數綁定到ASPX、ASHX和MVC的方法(菜鳥必看)的相關信息資訊供網民參考!
  • 推薦文章
    主站蜘蛛池模板: 天天色图片| 又黄?又爽刺激小说| 欧美精品人爱a欧美精品| 国产精品久久久久无码AV出租 | 男人猛躁女人秘?视频 | 国产黑丝视频| 被黑人玩弄H调教小说| 自拍偷拍亚洲视频| 张开腿我要上你h男男| 国产一级a毛一级a看免费视频黑人| 91精品国产综合久久久久久白拍 | 免费的三及片| 免费看黃色AAAA片软件 | 宝贝几天没c你了好爽菜老板| 老师下面太紧了我拔不出来| 久久99国产精| 成年女人喷潮毛片免费播放| 蜜桃成熟时2电影| 杨颖好深啊再用力一点| 欧美人与禽动交精品免费视频| 庄浪县| 女女les同性高h在线视频 | 欧美xxxxw| 中文字幕第4页| 白丝美女脱了内裤打开腿露出尿囗| 巨大?房乳?挤奶boos偷窥| 少妇做爰奶水狂喷av日本| 太大太粗好爽受不了p| 色香欲亚洲天天综合网| 中国????免费XXXX18| 陈德容三级露全乳照| 91丨九色丨国产熟女??熟女| 日本流氓片| 99在线视频免费观看| 女人自己扒荫道口视频| 麻豆国产秘?网站入口| 苗族一级特黄a大片| 五月伊人| 荷兰人牲禽动交另类| 老师扒开腿秘?让我爽了一夜软件| 老牛影视文化传媒有限公司官方|