HttpRequest 索引器的搜索顺序
-
06-07-2019 - |
题
如果您通过 Request[key] 对请求的项目进行简单索引,它会查找 4 个地点. 。有什么顺序吗?有人在该页面上猜测“Cookies、ServerVariables、Form 和 QueryString”。有人有确切消息么?文档将是一个额外的好处:)
解决方案
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;
}
}
不隶属于 StackOverflow