项目背景:在.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

HttpContext的dynamic包装器DynamicHttpContext (附原代码)的更多相关文章

  1. Fms3和Flex打造在线多人视频会议和视频聊天(附原代码)

    Flex,Fms3系列文章导航 Flex,Fms3相关文章索引 本篇是视频聊天,会议开发实例系列文章的第3篇,该系列所有文章链接如下: http://www.cnblogs.com/aierong/a ...

  2. SwiftUI 简明教程之属性包装器

    本文为 Eul 样章,如果您喜欢,请移步 AppStore/Eul 查看更多内容. Eul 是一款 SwiftUI & Combine 教程 App(iOS.macOS),以文章(文字.图片. ...

  3. Java基础(八)对象包装器与自动装箱

    1.对象包装器 有时候,需要将int这样的基本类型转换为对象.所有的基本类型都有一个与之对应的类.通常,这些类被称为包装器(wrapper). 这些对象包装类分别是:Integer.Long.Floa ...

  4. java基础类型、包装器

    char a = 'h';  //类包装器 Character aobj = a ;//自动装箱 byte b = 6; Byte bobj = b; short s = 234; Short sob ...

  5. C++11多态函数对象包装器

    [C++11多态函数对象包装器] 针对函数对象的多态包装器(又称多态函数对象包装器)在语义和语法上和函数指针相似,但不像函数指针那么狭隘.只要能被调用,且其参数能与包装器兼容的都能以多态函数对象包装器 ...

  6. ACE的包装器

    ACE大量运用包装器模式,以期改变面向过程的系统API可视性以及错误处理较难的情况 包装器在若干场合能极大简化代码量,甚至是编码过程 比如ACE_Thread_Mutex 对象创建会自动初始化,很开心 ...

  7. Java中基本数据类型和包装器类型的关系

    在程序设计中经常用到一系列的数据类型,在Java中也一样包含八中数据类型,这八种数据类型又各自对应一种包装器类型.如下表: 基本类型 包装器类型 boolean Boolean char Charac ...

  8. Java 装箱、拆箱 包装器

    先说java的基本数据类型.java基本数据类型:byte.short.int.long.float.double.char.boolean 基本数据类型的自动装箱(autoboxing).拆箱(un ...

  9. c#万能视频播放器(附代码)

    原文:c#万能视频播放器(附代码) c#万能视频播放器 本人之前很多的文章中均提到了使用libvlc为播放器内核制作的播放器,也许有些朋友对此感兴趣,于是我用c#写了一个调用libvlc api实现的 ...

随机推荐

  1. select distinct

    select distinct select distinct 用于返回表中唯一不同的值. 语法 select distinct 列名称 from 表名称 使用 distinct 关键字 Studen ...

  2. Callable与Future的介绍

    http://www.cnblogs.com/whgw/archive/2011/09/28/2194760.html

  3. Sort Colors [LeetCode]

    Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...

  4. Win服务器常用批处理脚本

    oracle数据库备份 先导出数据库,然后执行压缩,将源文件删除,保留压缩文件 exp crm/crm@orcl file=G:\数据库备份\CRM\CRM%DATE%.dmp owner=crm&q ...

  5. zoom和transform:scale的区别

    小tips: zoom和transform:scale的区别 这篇文章发布于 2015年11月3日,星期二,00:52,归类于 css相关. 阅读 7876 次, 今日 8 次 by zhangxin ...

  6. powerdesigner,eclipse整合安装

    com.sybase.powerdesigner.eclipse.link path=D:\\dbs\\dbtools\\SAP\\PowerDesigner16

  7. bzoj4213: 贪吃蛇

    题意:给定一个网格,有一些格子是障碍不用管,剩余的是空地,你要用一些起点和终点在边界上的路径或环来完全覆盖掉空地,如果使用第一种,会付出1的代价,求最小代价,不能覆盖则输出-1. 现在看到网格而且数据 ...

  8. JAVA类和对象课后作业

    1.使用类的静态字段和构造函数,我们可以跟踪某个类所创建对象的个数.请写一个类,在任何时候都可以向它查询"你已经创建了多少个对象?" 代码: //显示类 //YiMingLai 2 ...

  9. SET ANSI_NULLS ON ……

    SET QUOTED_IDENTIFIER ON   SET ANSI_NULLS ON    SET QUOTED_IDENTIFIER ON  GO  是什么意思?    语法  SET QUOT ...

  10. mysql-5.6.17-win32的安装?

    官方mysql最新版本:http://cdn.mysql.com/Downloads/MySQL-5.6/mysql-5.6.17.tar.gz 解压后,以管理员的身份打开cmd.exe,切入安装目录 ...