Search order of HttpRequest indexer

冷暖自知 提交于 2019-12-09 06:05:40

问题


If you do a simple index into Request's items via Request[key], it looks in 4 locations. What's the order? Someone makes a guess on that page at "Cookies, ServerVariables, Form and QueryString". Does anyone know for sure? Documentation would be a bonus :)


回答1:


public string this[string key] { get; }

Declaring Type: System.Web.HttpRequest Assembly: System.Web, Version=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:


Just use Reflector and you can see it for yourself. The order is QueryString, Form, Cookies, then ServerVariables.




回答3:


This is from an ASP site, but it still applies to ASP.NET:

All request object variables can be accessed directly by calling Request(variable) without the collection name. In this case, the Web server searches the collections in the following order:

  1. QueryString
  2. Form
  3. Cookies
  4. ClientCertificate
  5. ServerVariables


来源:https://stackoverflow.com/questions/1632969/search-order-of-httprequest-indexer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!