HttpContext的dynamic封裝器DynamicHttpContext (附原代碼)

來源:互聯網
上載者:User

標籤:style   blog   http   color   io   os   ar   使用   for   

項目背景:在.net framework下使用asp.net webform,特別是aspx+ajax+ashx中,ashx後台代碼擷取傳入參數的時候,需要很多[“…”],我用dynamic對他進行封裝。

廢話不多說,上代碼(文章最下面有上傳打包代碼):

using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;namespace MyWebDemo{    public partial class UserList : System.Web.UI.Page    {        /// <summary>        /// [email protected] qq:38798579        /// http://www.cnblogs.com/stevenchennet        /// </summary>        protected void Page_Load(object sender, EventArgs e)        {            //             // 這是以前的做法,這些["parameter"]很費勁            //            //HttpContext context = HttpContext.Current;            //if (!this.IsPostBack)            //{            //    // http://localhost:20000/UserList.aspx?id=1&name=StevenChennet            //    int id = int.Parse(context.Request.Params["id"]);            //    
// string name = context.Request.Params["nAMe"]; // this.lbl.InnerText = string.Format("Get提交 id:{0} name:{1}", id, name); //} //else //{ // // form post // int age = int.Parse(context.Request.Form["age"]); // string address = context.Request.Form["AddRESS"]; // this.lbl.InnerText = string.Format("Post提交 age:{0} address:{1}", age, address); //} dynamic dContext = new DynamicHttpContext(HttpContext.Current); if (!this.IsPostBack) { // http://localhost:20000/UserList.aspx?id=1&name=StevenChennet int id = dContext.id; // nAMe也可以用Name,忽略大小寫,這裡故意寫成nAMe意思是忽略大小寫。
string name = dContext.nAMe; this.lbl.InnerText = string.Format("Get提交 id:{0} name:{1}", id, name); } else { // form post int age = dContext.Age; string address = dContext.AddRESS; this.lbl.InnerText = string.Format("Post提交 age:{0} address:{1}", age, address); } } }}

  

下面是實現的代碼

 

using System;using System.Collections.Generic;using System.Dynamic;using System.Linq;using System.Web;namespace System.Web{    /// <summary>    /// [email protected] qq:38798579    /// http://www.cnblogs.com/stevenchennet    /// </summary>    public class DynamicHttpContext : DynamicObject    {        private string keyContent;        private HttpContext httpContext;        public DynamicHttpContext(HttpContext context)        {            this.httpContext = context;        }        public override bool TryGetMember(GetMemberBinder binder, out object result)        {            string key = binder.Name;            if (httpContext.Request.HttpMethod == "GET")            {                this.keyContent = this.httpContext.Request.QueryString.Get(key);            }            else            {                this.keyContent = this.httpContext.Request.Form.Get(key);            }            result = this;            return true;        }        public override bool TryConvert(ConvertBinder binder, out object result)        {            result = null;            Type binderType = binder.Type;            //int            if (binderType == typeof(int))            {                result = int.Parse(this.keyContent);            }            else if (binderType == typeof(int?))            {                if (string.IsNullOrWhiteSpace(this.keyContent))                {                    result = int.Parse(this.keyContent);                }                else                {                    result = default(int?);                }            }            // bool            else if (binderType == typeof(bool))            {                result = bool.Parse(this.keyContent);            }            else if (binderType == typeof(bool?))            {                if (string.IsNullOrWhiteSpace(this.keyContent))                {                    result = bool.Parse(this.keyContent);                }                else                {                    result = default(bool?);                }            }            // datetime            else if (binderType == typeof(DateTime))            {                result = DateTime.Parse(this.keyContent);            }            else if (binderType == typeof(DateTime?))            {                if (string.IsNullOrWhiteSpace(this.keyContent))                {                    result = DateTime.Parse(this.keyContent);                }                else                {                    result = default(DateTime?);                }            }            // string            else if (binderType == typeof(string))            {                result = this.keyContent;            }            else            {                throw new NotSupportedException(string.Format("類型 {0} 還未實現,請添加轉碼", binderType.ToString()));            }            return true;        }    }}
 

 

 

 

-----------------------利益相關:.net軟狗一枚,濟南工作,qq:38798579,歡迎同道朋友騷擾。----------------------

代碼打包下載(VS2013)

http://files.cnblogs.com/StevenChennet/AppDomainPerformanceDemo.zip

HttpContext的dynamic封裝器DynamicHttpContext (附原代碼)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.