【问题标题】:Search order of HttpRequest indexerHttpRequest 索引器的搜索顺序
【发布时间】:2009-10-27 19:02:02
【问题描述】:

如果您通过 Request[key] 对 Request 的项目进行简单的索引,它会在 4 locations 中查找。顺序是什么?有人对该页面的“Cookies、ServerVariables、Form 和 QueryString”进行了猜测。有人有确切消息么?文档将是一个奖励:)

【问题讨论】:

    标签: c# asp.net request httprequest


    【解决方案1】:

    public string this[string key] { get; }

    声明类型:System.Web.HttpRequest 程序集:System.Web, 版本=2.0.0.0

    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;
        }
    }
    

    【讨论】:

    【解决方案2】:

    只需使用Reflector,您就可以自己查看。顺序是 QueryString、Form、Cookies,然后是 ServerVariables。

    【讨论】:

      【解决方案3】:

      这是来自ASP site,但它仍然适用于 ASP.NET:

      所有请求对象变量都可以 通过调用直接访问 请求(变量)没有 集合名称。在这种情况下,网络 服务器搜索集合中的 以下顺序:

      1. 查询字符串
      2. 表格
      3. Cookie
      4. 客户证书
      5. 服务器变量

      【讨论】:

      • Reflector 说“ClientCertificate”不在 .Net 2.0 中搜索。
      猜你喜欢
      • 1970-01-01
      • 2018-07-12
      • 1970-01-01
      • 2017-12-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多