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
HttpContext的dynamic包装器DynamicHttpContext (附原代码)的更多相关文章
- Fms3和Flex打造在线多人视频会议和视频聊天(附原代码)
Flex,Fms3系列文章导航 Flex,Fms3相关文章索引 本篇是视频聊天,会议开发实例系列文章的第3篇,该系列所有文章链接如下: http://www.cnblogs.com/aierong/a ...
- SwiftUI 简明教程之属性包装器
本文为 Eul 样章,如果您喜欢,请移步 AppStore/Eul 查看更多内容. Eul 是一款 SwiftUI & Combine 教程 App(iOS.macOS),以文章(文字.图片. ...
- Java基础(八)对象包装器与自动装箱
1.对象包装器 有时候,需要将int这样的基本类型转换为对象.所有的基本类型都有一个与之对应的类.通常,这些类被称为包装器(wrapper). 这些对象包装类分别是:Integer.Long.Floa ...
- java基础类型、包装器
char a = 'h'; //类包装器 Character aobj = a ;//自动装箱 byte b = 6; Byte bobj = b; short s = 234; Short sob ...
- C++11多态函数对象包装器
[C++11多态函数对象包装器] 针对函数对象的多态包装器(又称多态函数对象包装器)在语义和语法上和函数指针相似,但不像函数指针那么狭隘.只要能被调用,且其参数能与包装器兼容的都能以多态函数对象包装器 ...
- ACE的包装器
ACE大量运用包装器模式,以期改变面向过程的系统API可视性以及错误处理较难的情况 包装器在若干场合能极大简化代码量,甚至是编码过程 比如ACE_Thread_Mutex 对象创建会自动初始化,很开心 ...
- Java中基本数据类型和包装器类型的关系
在程序设计中经常用到一系列的数据类型,在Java中也一样包含八中数据类型,这八种数据类型又各自对应一种包装器类型.如下表: 基本类型 包装器类型 boolean Boolean char Charac ...
- Java 装箱、拆箱 包装器
先说java的基本数据类型.java基本数据类型:byte.short.int.long.float.double.char.boolean 基本数据类型的自动装箱(autoboxing).拆箱(un ...
- c#万能视频播放器(附代码)
原文:c#万能视频播放器(附代码) c#万能视频播放器 本人之前很多的文章中均提到了使用libvlc为播放器内核制作的播放器,也许有些朋友对此感兴趣,于是我用c#写了一个调用libvlc api实现的 ...
随机推荐
- Laravel RuntimeException inEncrypter.php line 43: The only supported ciphers are AES-128-CBC and AES-256-CBC with the correct key lengths
php artisan key:generate 运行上面代码即可解决
- IDE编辑器编码配置
做跨平台开发时,大家用不同的IDE合作开发,最令人头疼的事就是各种乱码问题. 常用的IDE都支持utf-8编码和unix格式行尾'\n'. 1.XCODE设置文本编码及换行Xcode >> ...
- 51nod 1181 质数中的质数(质数筛法)
题目链接:51nod 1181 质数中的质数(质数筛法) #include<cstdio> #include<cmath> #include<cstring> #i ...
- 关于css
已经学了四天的css.现在对于css的了解还很肤浅,首先,我对基础的还不是很了解. 级联样式表(Cascading Style Sheet)简称“CSS”,通常又称为“风格样式表(Style Shee ...
- CheckLogin
# encoding: utf-8 # Creator:耿亚月 Creation time:2017-1-1 # Modifier:耿亚月 Modification time:2017-1-2 #fi ...
- 艺萌文件上传下载及自动更新系统(基于networkComms开源TCP通信框架)
1.艺萌文件上传下载及自动更新系统,基于Winform技术,采用CS架构,开发工具为vs2010,.net2.0版本(可以很容易升级为3.5和4.0版本)开发语言c#. 本系统主要帮助客户学习基于TC ...
- EntityFramework CodeFirst SQLServer转Oracle踩坑笔记
接着在Oracle中使用Entity Framework 6 CodeFirst这篇博文,正在将项目从SQLServer 2012转至Oracle 11g,目前为止遇到的问题在此记录下. SQL Se ...
- VS2003"无法启动调试 没有正确安装调试器"的解决办法
VS2003"无法启动调试 没有正确安装调试器"的解决方法 在用VS2003做项目的时候,经常调试程序,但是有时候回出现如下问题“无法启动调试,没有正确安装调试器,请运行安装程序或 ...
- 【PCB】【项目记录】AWG任意波形产生器
———————————————————————————————————————————————————————————————————— 原理图V1.0 01.top 已完成原理图绘制,但有相当错误, ...
- LeetCode 5 Longest Palindromic Substring manacher算法,最长回文子序列,string.substr(start,len) 难度:2
https://leetcode.com/problems/longest-palindromic-substring/ manacher算法相关:http://blog.csdn.net/ywhor ...