C# 之 Request

Request.QueryString(取得地址栏参数值)获取地址栏中的参数,意思就是取得”?"号后面的参数值.如果是多个是用这”&”符号连接起来的。Request.form取得表单参数值。

http://localhost/test.aspx?id=123
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;
        }
    }
原文地址:https://www.cnblogs.com/gavin-num1/p/4481687.html