locked
Can't determine original URL RRS feed

  • Question

  • User132352790 posted

    I am working on a REST style application and have run into an issue.  I want to know if it is possible to determine the original URL entered in the browser.  The specific case giving me problems involves URLs containing '+' characters.  For example, if the URL entered in the browser is:

     http://localhost/myapp/a+b?c+d=e

     then inside my HttpModule the Request.RawUrl looks like this:

     /myapp/a+b?c+d=e

     However, if the URL entered in the browser is:

     http://localhost/myapp/a%2bb?c%2bd=e

     then Request.RawUrl looks like this:

    /myapp/a+b?c%2bd=e

    (%2b is the URLencoded value for '+'.) So the base URL path is being automatically decoded, while the query string is not.  This makes it impossible for me to distinguish between

    http://localhost/myapp/a+b and

    http://localhost/myapp/a%2bb

     I want to treat the first case as a request for a resource named "a b" and the second case as a request for the resource "a+b".  Is there a way to distinguish between the two and why the discrepency in the handling of the values of the raw URL and the query string?

    Monday, July 16, 2007 4:59 PM

All replies

  • User1696511887 posted

    Request.Url.ToString() should give you the original url.  When I do a Response.Write(Request.Url.ToString()) it outputs +'s instead of the %2b stuff.

    Monday, July 16, 2007 5:37 PM
  • User132352790 posted

    The point is that I want the %2b, and not the '+' so I can tell the difference between when a "+" is used and "%2b" is used, just like it is for querystrings.

    Monday, July 16, 2007 7:37 PM
  • User1696511887 posted

    I'm not sure how you would do that.  Best of luck to you.

    Monday, July 16, 2007 8:24 PM
  • User-900677162 posted

    HI, Odegaard 

    You can use the following syntax toEncodes a URL string.

    HttpUtility.UrlEncode( Request.Url.ToString());

    http://msdn.microsoft.com/en-us/library/4fkewx0t.aspx.

    If i misunderstand you about your question, please feel free to correct me and i will try to help you with more information.

    I hope the above information will be helpful. If you have any issues or concerns, please let me know. It's my pleasure to be

    of assistance

     

    Tuesday, July 17, 2007 10:32 PM
  • User132352790 posted

    I guess the way I presented the question is a bit confusing so let me try to rephrase it.  I am only concerned with determining the original URL that the client of my web application used.

     I have implemented a REST style web application using a wild card mapping and a custom HttpModule such that any URL that starts with, for sake of example,

    http://abc/rest/

    will be processed by my HttpModule. So if someone typed in any of the following

    http://abc/rest/123/456/789
    http://abc/rest/some%20resource
    http://abc/rest/anything/you/can/think/of

    my HttpModule would be invoked and I can handle the request appropriately.  Now the problem I have is how do I, as the HttpModule author, distinguish between the following two cases:

    http://abc/rest/1+2
    http://abc/rest/1%2b2

    From the code that I write inside of the HttpModule, every property that is available to me on the HttpContext.Request object that I have looked at shows the above 2 URLs both to be http://abc/rest/1+2.  It always appears that the client used a '+' in the URL, I never see the "%2b".  I need to handle the above 2 cases separately.  Is it even possible to distinguish between the two from the perspective of my HttpModule?

     In a nutshell: %2b or not %2b, that is the question.

    Wednesday, July 18, 2007 11:48 AM
  • User-900677162 posted

    Hi,

    I got it. It seems to be a question in the the HttpModule

    What i have provided is just a small step you can have a look:

     In one particular case, I manufacture all of the Urls that the user accesses using
    Url rewriting, and as a result I can eliminate the problematic "space to +"
    encoding behavior.

    // original Windows filename I need to embed in the Url
    string szUrl = "test+test test.txt";

    // encode the Url
    szUrl = Server.Encode (szUrl); // creates test%2Btest+test

    // substitute %20 for + as encoded spaces
    szUrl = szUrl.Replace ("+", "%20"); // creates test%2Btest%20test

    // finish Url
    szUrl = "http://mysite.com/getfile/" + szUrl;

    // set link
    hlFile.Hyperlink = szUrl;


    In this case the HttpModule gets the Url as;

    http://mysite.com/getfile/test+test test.txt

    If i misunderstand you about your question, please feel free to correct me and i will try to help you with more information.

    I hope the above information will be helpful. If you have any issues or concerns, please let me know. It's my pleasure to be

    of assistance

    Wednesday, July 18, 2007 10:36 PM
  • User132352790 posted

    I don't have any issues creating links as I'm in complete control there.  Incidentally, I use HttpUtility.UrlPathEncode when formatting links and everything works fine.  The only problem is that clients can construct their own URLs based on knowledge of the name of the resource they are interested.  I can't prevent a client from using a '+' to represent a space in the resource name.  This may simply need to be a documented requirement of URLs that the application can process, but I just really want to know if there is a way to determine the original URL that the client requested.

    Thursday, July 19, 2007 11:36 AM