[「身份證」]
回答
Request["id"]
從QueryString
,Form
,Cookies
或ServerVariables
集合獲得值。它們的搜索順序沒有在文檔中指定,但是當您查看源代碼時,您會看到它們的提及順序。
因此,如果您知道變量駐留的位置,您通常會這樣做,最好使用更具體的選項。
Request.QueryString["id"]
會返回一個項目的價值在於有id
一鍵查詢字符串,而Request["id"]
將的Request.QueryString,的Request.Form,Request.Cookies時,或Request.ServerVariables之一返回一個項目。
值得一提的是,documentation的Request.Item
(這是你當你調用Request["id"]
實際上正在訪問)不不指定這些藏品將被搜索的順序,所以理論上你就可以得到不同的結果取決於在哪個版本的asp.net上運行。
如果您知道所需的值是在您的查詢字符串,它總是最好使用Request.QueryString["id"]
來訪問它,而不是Request["id"]
。
按照documentation的HttpRequest
索引
查詢字符串,形式,在鍵參數指定餅乾,或 ServerVariables集合構件 。
我更喜歡使用Request.QueryString["id"]
,因爲它的值更加明確。
Request.QueryString [「id」]查看通過每個QueryString傳遞的集合。 Request.Item [「id」]查看所有集合(QueryString,Form,Cookies或ServerVariables)。因此,如果可能的話,QueryString屬性應該是首選的,因爲它比較小。
Request集合是QueryString的超集,還有一些與當前請求相關的更多數據。
爲了「更好」 - 我建議你準確和明確地使用(即使用QueryString)來避免意外的因素,當你得到意想不到的結果只是意識到你使用了一個給定的鍵請求未提供查詢字符串值,但它存在於其他一些集合中。
+1用於回答整個問題。我當然看到,當某些事情從cookie中讀取而不是查詢字符串時,代碼會感到困惑。 – 2010-08-10 12:11:01
據Reflector.Net,請求[「ID」]被定義爲:
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;
}
}
- 1. Microsoft身份證與身份證DI
- 2. WSO2身份服務器身份驗證
- 3. 身份驗證證書
- 4. OWIN身份驗證
- 5. Orchard:Facebook身份驗證
- 6. DotVVM身份驗證
- 7. SaaS身份驗證
- 8. WCF身份驗證
- 9. OpenLDAP身份驗證
- 10. Vagrant身份驗證
- 11. SWFUpload身份驗證
- 12. Webservice身份驗證
- 13. Firebase身份驗證
- 14. Mongo身份驗證
- 15. ExtJS身份驗證
- 16. WCF身份驗證
- 17. SOAP身份驗證
- 18. 身份驗證base64
- 19. Rails身份驗證
- 20. Smtp身份驗證
- 21. SymbolSource身份驗證
- 22. WCF身份驗證
- 23. JSF身份驗證
- 24. Django - 身份驗證
- 25. umbraco身份驗證
- 26. 身份驗證Cookie
- 27. Mongodb身份驗證
- 28. Nagios身份驗證
- 29. 身份驗證Android
- 30. ajax身份驗證
-1從我的,就目前而言,如文檔*不*指定在集合的訪問順序。如果文檔與實現相匹配,那就是純粹的運氣,正如我在答案中指出的那樣,它可能會改變。刪除「按此順序」,我將非常樂意刪除已刪除的-1 =) – Rob 2010-08-10 11:39:13
-1,http://msdn.microsoft.com/en-us/library/ms524948上還有一些文檔%28VS.90%29.aspx(感謝@John的回答指出了這一點),這表明集合是按特定順序搜索的。如果MS在專門爲Request.Item提供的文檔中提到過,那會很好,呃! : -/ – Rob 2010-08-10 11:40:59
我刪除了'按此順序'部分,因爲這是未記錄的。然而,看看源代碼,你會發現它使用指定的順序。但你是對的,這是一個實現細節:) – 2010-08-10 11:54:29