Request[id]的作用
- 格式:pdf
- 大小:56.32 KB
- 文档页数:1
Request[id]的作⽤
Request.QueryString 替我们件事情:每次接受到参数后,都做 UrlEncode ,并且是按照 UTF-8编码做的 UrlEncode 。 这在⼤多数情况下
没有任何问题,但是⼀些情况下,会给我们带来⿇烦,本⽂就是分析这些可能给我们带来⿇烦的场景,以及解决⽅法。
Request.QueryString["id"] 只能读取通过地址栏参数传递过来的名为id的参数。
Request["id"]是⼀个复合功能读取函数。
它的优先级顺序为QueryString > Form > Cookies > ServerVariables
也就是说,如果存在名为id的地址栏参数,Request[ "id" ] 的效果和 Request.QueryString["id"] 是样的。
如 果不存在名为id的地址栏参数,Request.QueryString["id"]将会返回空,但是Request[ "id" ]会继续检查是否存在名为id的表单提交元素,
如果不存在,则继续尝试检查名为id的Cookie,如果不存在,继续检查名为id的服务器环境变量。它将 最多做出4个尝试,只有四个尝试都
失败,才返回空。
以下是Request[ "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;
}
}