佳木斯湛栽影视文化发展公司

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

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

熱門標簽:科大訊飛語音識別系統(tǒng) Mysql連接數(shù)設置 Linux服務器 阿里云 團購網(wǎng)站 電子圍欄 服務器配置 銀行業(yè)務

前言

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

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

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

標簽:江蘇 廣元 萍鄉(xiāng) 大理 衢州 蚌埠 棗莊 衡水

巨人網(wǎng)絡通訊聲明:本文標題《基于.NET中:自動將請求參數(shù)綁定到ASPX、ASHX和MVC的方法(菜鳥必看)》,本文關鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權問題,煩請?zhí)峁┫嚓P信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡,涉及言論、版權與本站無關。
  • 相關文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266
    仙居县| 连江县| 哈密市| 湘乡市| 招远市| 盐源县| 蓝山县| 卫辉市| 宜丰县| 尚义县| 佛坪县| 大冶市| 达州市| 巨鹿县| 遂宁市| 麦盖提县| 滨海县| 南部县| 永年县| 东宁县| 绵阳市| 乡城县| 桃园县| 台北市| 庆元县| 桃江县| 永善县| 阿鲁科尔沁旗| 湖南省| 巴南区| 丹江口市| 成武县| 平顶山市| 荆门市| 阳山县| 玉田县| 通海县| 苍南县| 彭州市| 祁连县| 吉林市|