Request[]与Request.Params[] 差别

Request[]与Request.Params[] ,这二个属性都可以让我们方便地根据一个KEY去【同时搜索】QueryString、Form、Cookies 或 ServerVariables这4个集合

这二个属性唯一不同的是:Item是依次访问这4个集合,找到就返回结果,而Params是在访问时,先将4个集合的数据合并到一个新集合(集合不存在时创建), 然后再查找指定的结果。

Request[]实现原理(.net源代码)

// System.Web.HttpRequest
/// <summary>Gets the specified object from the <see cref="P:System.Web.HttpRequest.QueryString" />, <see cref="P:System.Web.HttpRequest.Form" />, <see cref="P:System.Web.HttpRequest.Cookies" />, or <see cref="P:System.Web.HttpRequest.ServerVariables" /> collections.</summary>
/// <returns>The <see cref="P:System.Web.HttpRequest.QueryString" />, <see cref="P:System.Web.HttpRequest.Form" />, <see cref="P:System.Web.HttpRequest.Cookies" />, or <see cref="P:System.Web.HttpRequest.ServerVariables" /> collection member specified in the <paramref name="key" /> parameter. If the specified <paramref name="key" /> is not found, then null is returned.</returns>
/// <param name="key">The name of the collection member to get. </param>
public string this[string key]
{
    get
    {
        string text = this.QueryString[key];
        if (text != null)
        {
            return text;
        }
        text = this.Form[key];
        if (text != null)
        {
            return text;
        }
        HttpCookie httpCookie = this.Cookies[key];
        if (httpCookie != null)
        {
            return httpCookie.Value;
        }
        text = this.ServerVariables[key];
        if (text != null)
        {
            return text;
        }
        return null;
    }
}

Request.Params[]源码

// System.Web.HttpRequest
/// <summary>Gets a combined collection of <see cref="P:System.Web.HttpRequest.QueryString" />, <see cref="P:System.Web.HttpRequest.Form" />, <see cref="P:System.Web.HttpRequest.Cookies" />, and <see cref="P:System.Web.HttpRequest.ServerVariables" /> items.</summary>
/// <returns>A <see cref="T:System.Collections.Specialized.NameValueCollection" /> object. </returns>
public NameValueCollection Params
{
    get
    {
        if (HttpRuntime.HasAspNetHostingPermission(AspNetHostingPermissionLevel.Low))
        {
            return this.GetParams();
        }
        return this.GetParamsWithDemand();
    }
}
// System.Web.HttpRequest
private NameValueCollection GetParams()
{
    if (this._params == null)
    {
        this._params = new HttpValueCollection(64);
        this.FillInParamsCollection();
        this._params.MakeReadOnly();
    }
    return this._params;
}
// System.Web.HttpRequest
private void FillInParamsCollection()
{
    this._params.Add(this.QueryString);
    this._params.Add(this.Form);
    this._params.Add(this.Cookies);
    this._params.Add(this.ServerVariables);
}

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

时间: 2024-08-28 01:08:37

Request[]与Request.Params[] 差别的相关文章

细说 Request[]与Request.Params[]

细说 Request[]与Request.Params[] 阅读目录 开始 回顾博客原文 实现方式分析 再谈Cookie 再谈NameValueCollection 再谈QueryString, Form 如何处理冲突 Request[]还是Request.Params[] ?? 今天我来谈一谈容易被人混淆的二个集合:Request[]与Request.Params[] 这二个集合我在博客[我心目中的Asp.net核心对象]中就提到过它们, 而且还给出了一个示例,并以截图的形式揭示过它们的差别.

细说Request与Request.Params

在ASP.NET编程中,有三个比较常见的来自于客户端的数据来源:QueryString, Form, Cookie .我们可以在HttpRequest中访问这三大对象,比如,可以从QueryString中获取包含在URL中的一些参数,可以从Form中获取用户输 入的表单数据,可以从Cookie中获取一些会话状态以及其它的用户个性化参数信息.除了这三大对象,HttpRequest还提供 ServerVariables来让我们获取一些来自于Web服务器变量.通常,这4个数据来源都很明确,我想没人会混

客户端的数据来源:QueryString, Form, Cookie Request[]与Request.Params[]

在ASP.NET编程中,有三个比较常见的来自于客户端的数据来源:QueryString, Form, Cookie . 我们可以在HttpRequest中访问这三大对象. QueryString: 获取包含在URL中的一些参数: 获取get方式提交的表单数据 (在提交表示如不指定method类型,则默认为get方式) Form: 获取post方式提交的表单数据 Cookie: 获取一些会话状态以及其它的用户个性化参数信息. 除了这三大对象,HttpRequest还提供ServerVariable

Request,Request.QueryString,Request.Params,Request.Form,Request.ServerVariables之间的区别

常见的aspx页面传值方式 1.get方式 发送页面:<a href="RequestPage.aspx?name=value"></a> 接收页面:Request["name"],Request.QueryString["name"],Request.Params["name"] 2.post方式 发送页面: <form mathod="post" action="

400 Bad Request The request sent by the client was syntactically incorrect ().

项目中一直出现400错误,后面搜索下,发现如下内容. SpringMVC报错信息为The request sent by the client was syntactically incorrect () 在数据绑定的时候一定要主意Controller方法中的参数名和jsp页面里的参数名字是否一致或者按照绑定的规范来写,如果不一致,可能回报如下错误: The request sent by the client was syntactically incorrect (). 从字面上理解是:客户

org.apache.catalina.connector.Request.parseParameters(Request.java:2446) NullPointerException异常处理

1.环境 tomcat6.0.8 + jdk6 + struts1 2.问题的产生 页面有两个异常的请求 http://127.0.0.1/project/a.action?do=testA&m=10 http://127.0.0.1/project/a.action?do=testB&m=10 两个请求几乎同步到达server.结果出现异常: java.lang.NullPointerException at org.apache.catalina.connector.Request.p

Request.Form和Request和Request.querystring的区别

Request.Form是获取以POST方式提交的表单数据: Request.QueryString主要是获取地址栏参数或者以Get方式提交的数据 而Request则包含以上两种方式,会在Request.QueryString和Request.Form中都查询一遍变量.但是优先获取GET方式提交的数据,即Request.QueryString Request:包含以上两种方式(优先获取GET方式提交的数据),它会在QueryString.Form.ServerVariable中都搜寻一遍. 而且

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

Request.Form:获取以POST方式提交的数据. Request.QueryString:获取地址栏参数(以GET方式提交的数据). Request:包含以上两种方式(优先获取GET方式提交的数据),它会在QueryString.Form.ServerVariable中都搜寻一遍. 有时候会得到不同的结果.如果仅仅需要Form中的数据,但是使用了Request而不是Request.Form,那么程序将在QueryString.ServerVariable中也搜寻一遍.如果其中有同名的项,

request.get request.GET……

发现他们是不同的. 报错: AttributeError at /add/ 'WSGIRequest' object has no attribute 'get' Request Method: GET Request URL: http://127.0.0.1:8000/add/?a=4&b=5 Django Version: 1.11 Exception Type: AttributeError Exception Value: 'WSGIRequest' object has no att