HttpRequest.Item 属性 和 HttpRequest.QueryString 属性的区别!

 

HttpRequest.Item 属性 和 HttpRequest.QueryString 属性的区别!

MSDN 关于这两个属性的说明


     属性:HttpRequest.Item

    说明:从 Cookies、Form、QueryString 或 ServerVariables 集合中获取指定的对象。


    属性:HttpRequest.QueryString 

    说明: 获取 HTTP 查询字符串变量集合。


内部实现    

    用 Reflector 反编译 System.Web.dll 。查看 HttpRequest 类的属性 Item 的实现代码如下:

代码1-1 

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

    从代码中,我们很清楚的看到, HttpRequest.Item 属性的内部实现:    

    1, 获取 HTTP 查询字符串变量集合。如果不存在执行第2步    

    2, 获取窗体变量集合。如果不存在则执行第3步    

    3, 获取客户端发送的 cookie 的集合。如果不存在则执行第4步  

    4, 获取 Web 服务器变量的集合。 如果不存在则返回 null    

    End。


原文地址:https://www.cnblogs.com/bruceleeliya/p/1506019.html