zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    Java 正则表达式的总结和一些小例子
    jquery用div模拟一个下拉列表框
    jquery点击div以外的区域触发事件
    Jackson 框架,轻易转换JSON
    Java Json API:Gson使用简单入门
    linux必学
    Python2 long() 函数
    Python int() 函数
    PyCharm快捷键大全
    PyCharm常用设置
  • 原文地址:https://www.cnblogs.com/imust2008/p/5407842.html
Copyright © 2011-2022 走看看