简体   繁体   中英

Getting “RawUrl” with square bracket notation from System.Web.HttpRequest

I'm running into an issue where I'm trying to get a member of an instance of the System.Web.HttpRequest class whenever a page is loaded in C#. Specifically the issue comes up whenever accessing the RawUrl member. Here's a simplified example, running on .Net 3.5.

string u0 = Request["RawUrl"]; // this gives me a value of null
string u1 = Request.RawUrl; // Using dot notation instead of square brackets works
string u2 = Request["Url"]; // However, "Url" works with square bracket notation

So my question is why is it that I can get the Url property of the Request with square bracket notation but can't do the same with RawUrl ? The Url property is a System.Uri property whereas RawUrl is just a string so I'd imagine that it would be even easier to get. What am I not understanding?

The HttpRequest indexer (how you access with the square brackets) accesses strings from the following locates in order.

  1. QueryString
  2. FormCollection
  3. Cookies
  4. ServerVariables

I have copy pasted the code (decompiled) for the indexer of HttpRequest at the bottom of this post.

Now to answer your question. The RawUrl is not part of any of those items but from a completly different location. The RawUrl is constructed by using two parts if valid.

  1. The QueryString text
  2. The Url string (unvalidated)

The code for the RawUrl construction spans over multiple classes and objects based on the request type. If you need more information on how the RawUrl is constructed I suggest you grab a decompiler and have a look.

Hope this helps

HttpRequest indexer code from System.Web.dll (v4.5)

public string this[string key]
{
    get
    {
        string item = this.QueryString[key];
        if (item != null)
        {
            return item;
        }
        item = this.Form[key];
        if (item != null)
        {
            return item;
        }
        HttpCookie httpCookie = this.Cookies[key];
        if (httpCookie != null)
        {
            return httpCookie.Value;
        }
        item = this.ServerVariables[key];
        if (item != null)
        {
            return item;
        }
        return null;
    }
}

It doesn't work because they are two completely different things. Having one doesn't automatically give you the other. Eg:

class YourClass {
    public string RawUrl { get; set; }
}

..gives you YourClass.RawUrl . Whereas:

class YourClass {
    public string this[string key] {
        get {
            return ...;
        }
    }
}

..gives you YourClass["RawUrl"] .

In terms of the HttpRequest object in ASP.NET.. it simply does not provide for "RawUrl" to be passed as a string. What it does internally, is uses the supplied string to check the following:

  • Request.QueryString
  • Request.Cookies
  • Request.Form
  • Request.ServerVariables

You can use Request["HTTP_URL"] (which is a server variable) to get roughly the equivalent of the RawUrl property.. you may have to combine it with other things though too.

Unlike eg JavaScript where foo["bar"] and foo.bar are to ways to access the same member, in C# they mean completely different things - it's not just a different notation, it's a completely different method.

The square-brackets on an object call the indexer if the object has one (and lead to a compiler error if the object doesn't). In the case of HttpRequest, the square brackets call into HttpRequest.Items .

Gets the specified object from the QueryString, Form, Cookies, or ServerVariables collections.

public string this[string key] { get; }

The URL is part of said items, the RawUrl is not. (That of course doesn't answer the question "Why is it not?" which I assume someone else has a good answer to, but my point is to address the possible misinformation that square brackets and dot notation are equivalent in C#)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM