在ASP.NET编程中,有三个比较常见的来自于客户端的数据来源:QueryString, Form, Cookie 。 我们可以在HttpRequest中访问这三大对象。

QueryString: 获取包含在URL中的一些参数; 获取get方式提交的表单数据 (在提交表示如不指定method类型,则默认为get方式)

Form: 获取post方式提交的表单数据

Cookie: 获取一些会话状态以及其它的用户个性化参数信息。

除了这三大对象,HttpRequest还提供ServerVariables来让我们获取一些来自于Web服务器变量。

一般情况下,如果我们在事先就能明确知道某个参数是来源于哪个集合,那么直接访问那个集合,问题也就简单了。 然而,更常见的数据来源通常只会是QueryString, Form ,而且尤其是当在客户端使用Jquery的$.ajax()这类技术时, 可以很随意地将参数放到QueryString或者是Form中,那么,服务端通常为了也能灵活地应对这一现况, 可以使用Request[]与Request.Params[] 这二种方式来访问这些来自于用户提交的数据。然而 Request[]与Request.Params[] 有什么差别??

public string this[string key]
{
get
{
string str = this.QueryString[key];
if( str != null ) {
return str;
}
str = this.Form[key];
if( str != null ) {
return str;
}
HttpCookie cookie = this.Cookies[key];
if( cookie != null ) {
return cookie.Value;
}
str = this.ServerVariables[key];
if( str != null ) {
return str;
}
return null;
}
}

Request 实现方式

这段代码的意思是:Request 根据指定的key,依次访问QueryString,Form,Cookies,ServerVariables这4个集合,如果在任意一个集合中找到了,就立即返回。

public NameValueCollection Params
{
get
{
//if (HttpRuntime.HasAspNetHostingPermission(AspNetHostingPermissionLevel.Low))
//{
// return this.GetParams();
//}
//return this.GetParamsWithDemand(); // 为了便于理解,我注释了上面的代码,其实关键还是下面的调用。
return this.GetParams();
}
}
private NameValueCollection GetParams()
{
if( this._params == null ) {
this._params = new HttpValueCollection(0x40);
this.FillInParamsCollection();
this._params.MakeReadOnly();
}
return this._params;
}
private void FillInParamsCollection()
{
this._params.Add(this.QueryString);
this._params.Add(this.Form);
this._params.Add(this.Cookies);
this._params.Add(this.ServerVariables);
}

Request.Params 的实现方式

它的实现方式是:Request.Params 先判断_params这个Field成员是否为null,如果是,则创建一个集合,并把QueryString,Form,Cookies,ServerVariables这4个集合的数据全部填充进来, 以后的查询都直接在这个集合中进行。

并且在网页中写入cookie的时候,requset.Params也会发生变化(具体原因不做陈述,可参考作者原文)

实例:

新建一个context.aspx网页

<head runat="server">
<title></title>
</head>
<body>
<p>Item结果:<%= this.ItemValue %></p>
<p>Params结果:<%= this.ParamsValue %></p> <hr /> <form action="<%= Request.RawUrl %>" method="post">
<input type="text" name="name" value="" />
<input type="submit" value="提交" />
</form>
</body>

前台代码

public partial class context : System.Web.UI.Page
{
protected string ItemValue;
protected string ParamsValue; protected void Page_Load(object sender, EventArgs e)
{
string[] allkeys = Request.QueryString.AllKeys;
if (allkeys.Length == )
Response.Redirect("context.aspx?name=abc", true); ItemValue = Request["name"];
ParamsValue = Request.Params["name"];
}
}

后台代码

网页提交前: 

网页提交后: 

原因是在使用Request[]来获取一个key的时候,会首先检索Questring,在检索到这个key的时候即返回,所以在提交后Item的结果依然网页url后面的name

而Request.Params[] 会将所有符合条件的key全部添加进入,所以在提交后,Params的结果是:url后面的name+input的name

上面的怪异的结果有时并不只是Params会有,同样的QueryString, Form 也会存在。

  如url的传递参数

protected void Page_Load(object sender, EventArgs e)
{
string[] allkeys = Request.QueryString.AllKeys;
if( allkeys.Length == )
Response.Redirect(
Request.RawUrl + "?aa=1&bb=2&cc=3&aa=" + HttpUtility.UrlEncode("5,6,7"), true); StringBuilder sb = new StringBuilder();
foreach( string key in allkeys )
sb.AppendFormat("{0} = {1}<br />",
HttpUtility.HtmlEncode(key), HttpUtility.HtmlEncode(Request.QueryString[key])); this.labResult.Text = sb.ToString();
}

QueryString

输出结果为:

aa = 1,5,6,7
bb = 2
cc = 3

  如在表单提交中 多个控件公用一个name的情况。

QueryString, Form,Param都有这样的冲突问题,只是Param需要合并4种数据源,它将冲突的机率变大了。

在使用时的建议是:

1. 不建议使用Params[],因为:a. 不希望被偶然情况影响,b. 较高的使用成本。
2. Request[] 使用简单,适合要求不高的场合:示例代码。
3. 如果需要兼顾性能和易用性,可以模仿Request[]自行设计。(通常并不需要访问4个集合)
4. 了解差异,体会细节,有时会发现它还是有利用价值的。

网页引用:http://www.cnblogs.com/fish-li/archive/2011/12/06/2278463.html

客户端的数据来源:QueryString, Form, Cookie Request[]与Request.Params[]的更多相关文章

  1. 细说Request与Request.Params

    在ASP.NET编程中,有三个比较常见的来自于客户端的数据来源:QueryString, Form, Cookie .我们可以在HttpRequest中访问这三大对象,比如,可以从QueryStrin ...

  2. Request对象主要用于获取来自客户端的数据,如用户填入表单的数据、保存在客户端的Cookie等。

    1.主要属性  ApplicationPath  获取服务器上asp.net应用程序的虚拟应用程序根路径  Browser  获取有关正在请求的客户端的浏览器功能的信息,该属性值为:HttpBrows ...

  3. Request、Request.Form和Request.QueryString的区别

    Request.Form:获取以POST方式提交的数据(接收Form提交来的数据): Request.QueryString:获取地址栏参数(以GET方式提交的数据) Request:包含以上两种方式 ...

  4. asp.net Request、Request.Form、Request.QueryString的区别(转)

    Request.Form:获取以POST方式提交的数据. Request.QueryString:获取地址栏参数(以GET方式提交的数据). Request:包含以上两种方式(优先获取GET方式提交的 ...

  5. [转载]Request、Request.Form和Request.QueryString的区别

    Request.Request.Form和Request.QueryString的区别 request本身是一个系统的静态对象,本身也可以作为数组调用,比如request("abc" ...

  6. 百度编辑器ueditor通过ajax方式提交,不需要事先转义字符的方法(异常:从客户端(xxx)中检测到有潜在危险的 Request.Form 值)

    最近项目中使用百度编辑神器ueditor,确实是很好用的一款编辑器.官网教程提供的与后端数据交互都是跟表单方式有关的,项目中使用的是ajax方式提交,因此出现了不少问题,现在记录备忘下. 环境:.ne ...

  7. request和request.form和request.querystring的区别

    asp中获取传递的参数,一般用request或者用request成员函数request.form,两种方式都可以获取页面表单传递过来的参数值,一直没留意两种方法有什么区别,我一般喜欢用request( ...

  8. C#中 Request, Request.params , Request.querystring , Request.Form 区别 与联系用法

    C#中 Request, Request.params , Request.querystring , Request.Form 区别 与联系用法? Request.params , Request ...

  9. Request.QueryString()和Request()和Request.Form();

    一. querystring的用法及原理 当页面上的 FORM以 GET方式向页面发送请求数据 (如数据含有不安全字符,则浏览器先将其转换成 16进制的字符再传送,如空格被转成 %20)时, WEB ...

随机推荐

  1. 《Prism 5.0源码走读》Service Locator Pattern

    在Prism Bootstrapper里面取实例的时候使用 ServiceLocator模式,使用的是CommonServiceLocator库 (http://commonservicelocato ...

  2. 数组(Array),二维数组,三维数组

    数组(Array):相同类型数据的集合就叫做数组. (一)定义数组的方法: A) type[] 变量名 = new type[数组中元素的个数] 例如: int[] a = new int[10] ; ...

  3. 263. Ugly Number

    Write a program to check whether a given number is an ugly number. Ugly numbers are positive numbers ...

  4. Python 3.5.2建立与DB2的连接

    Python是可以连接数据库,并从数据库获取相应的数据库的,但是怎么连接呢? 这是个问题,以下是我使用Python建立数据库连接的步骤(我使用的工具为:PyCharm) 1.首先下载setuptool ...

  5. Python常用内建模块

    Python常用内建模块 datetime 处理日期和时间的标准库. 注意到datetime是模块,datetime模块还包含一个datetime类,通过from datetime import da ...

  6. poj 3641 Pseudoprime numbers

    题目连接 http://poj.org/problem?id=3641 Pseudoprime numbers Description Fermat's theorem states that for ...

  7. hdu 1195 Open the Lock

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1195 Open the Lock Description Now an emergent task f ...

  8. ios中如何计算(页数,行数,等等的算法)

    页数 = (总个数 + 每页最大显示个数 - 1) / 每页显示最大的个数

  9. nodejs for centos配置

    mongodb http://www.cnblogs.com/zhoulf/archive/2013/01/31/2887439.html nodejs http://zhaohe162.blog.1 ...

  10. 前端开发规范之html编码规范

    原则1.规范 .保证您的代码规范,趋html5,远xhtml,保证结构表现行为相互分离.2.简洁.保证代码的最简化,避免多余的空格.空行,保持代码的语义化,尽量使用具有语义的元素,避免使用样式属性和行 ...