HttpContext Dynamic Wrapper Dynamichttpcontext (attached original code)

Source: Internet
Author: User

Project background: Using ASP. NET WebForm, especially in Aspx+ajax+ashx, requires a lot of ["..."] when ashx backend code gets incoming parameters, and I wrap it with dynamic.

Not much nonsense to say, on the code (the article at the bottom of the upload package code):

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 {//<s ummary>///[email protected] qq:38798579//http://www.cnblogs.com/stevenchennet//</summar y> protected void Page_Load (object sender, EventArgs e) {/////This is the previous practice, these ["Parame            Ter "] very laborious////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 Commit 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 submission 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 can also be used with the name, ignoring case, which is intentionally written in name meaning ignoring case.
String name = Dcontext.name; This.lbl.InnerText = string. Format ("Get Commit id:{0} Name:{1}", ID, name); } else {//form post int age = Dcontext.age; string address = dcontext.address; This.lbl.InnerText = string. Format ("Post submission age:{0} Address:{1}", age, address); } } }}

Here's the code for the implementation

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 = Bin Der.            Name; if (HttpContext.Request.HttpMethod = = "GET") {this.keycontent = This.httpContext.Request.QueryS Tring.            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 = Boo            L.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)) {ResU            lt = 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 ("type {0} is not yet implemented, add conversion code", Bindertype.tostring ()));        } return true; }    }}

-----------------------interest Related:. NET soft Dog One, Jinan work, qq:38798579, welcome fellow friends harassment. ----------------------

Code package download (VS2013)

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

HttpContext Dynamic Wrapper Dynamichttpcontext (attached original code)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.