国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

香港云服务器
服務器之家 - 編程語言 - ASP.NET教程 - 基于.NET中:自動將請求參數綁定到ASPX、ASHX和MVC的方法(菜鳥必看)

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

2019-11-05 10:53asp.net教程網 ASP.NET教程

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

前言

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


運行結果

 

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

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

延伸 · 閱讀

精彩推薦
529
主站蜘蛛池模板: 国产日韩精品一区二区 | 欧美日韩三级在线 | 亚洲高清电影 | 中日韩欧美风情视频 | 一级欧美在线 | 九九热在线免费视频 | 亚洲精品免费在线 | 国产色婷婷 | 91亚洲精品一区 | av在线入口| 亚洲精品乱码久久久久久金桔影视 | 久久香蕉国产 | 午夜精品久久久久久久99黑人 | 九九热在线免费视频 | 日本中文字幕在线观看 | 亚洲视频一区 | 色女网 | 亚洲一区在线免费观看 | 日本视频在线播放 | 女人久久久久 | 久久久精品天堂 | 成人在线一区二区三区 | 国产亚洲精品美女久久久久久久久久 | 色网在线看 | 久久久www成人免费精品 | 视频在线一区 | 亚洲高清在线 | 国产精品亚洲一区 | 国产性猛交xxxx免费看久久 | 久久久久久综合 | 青草福利 | 天天艹视频 | 亚洲一区二区中文字幕 | 91亚洲国产成人久久精品网站 | 亚洲一区精品在线 | 99视频在线免费观看 | 国产欧美日韩 | 在线一级黄色片 | 高清国产午夜精品久久久久久 | 性欧美精品久久久久久久 | 在线观看不卡 |