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


項目背景:在.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>
        /// StevemChennet@live.com 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>
    /// StevemChennet@live.com 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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM