Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Search order of HttpRequest indexer

If you do a simple index into Request's items via Request[key], it looks in 4 locations. What's the order? Someone makes a guess on that page at "Cookies, ServerVariables, Form and QueryString". Does anyone know for sure? Documentation would be a bonus :)

like image 864
JustLoren Avatar asked Oct 27 '09 19:10

JustLoren


2 Answers

public string this[string key] { get; }

Declaring Type: System.Web.HttpRequest Assembly: System.Web, Version=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;
    }
}
like image 81
rick schott Avatar answered Oct 16 '22 15:10

rick schott


Just use Reflector and you can see it for yourself. The order is QueryString, Form, Cookies, then ServerVariables.

like image 41
David Avatar answered Oct 16 '22 15:10

David