2009-10-27 103 views
6

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

回答

6

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

又一个有用的参考:http://www.hanselman.com/blog/ASPNETParams CollectionVsQueryStringFormsVsRequestindexAndDoubleDecoding.aspx – smwikipedia 2013-11-12 06:15:54

1

只需使用Reflector,你可以看到它自己。顺序是QueryString,Form,Cookies,然后是ServerVariables。

1

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

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

  1. 查询字符串
  2. 形式
  3. 饼干
  4. ClientCertificate
  5. ServerVariables
+2

反射器说在.Net 2.0中不搜索“ClientCertificate”。 – David 2009-10-27 19:34:40