Last post Jul 19, 2007 11:36 AM by Odegaard
Jul 16, 2007 04:59 PM|Odegaard|LINK
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:
then inside my HttpModule the Request.RawUrl looks like this:
However, if the URL entered in the browser is:
then Request.RawUrl looks like this:
(%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
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
Jul 16, 2007 05:37 PM|Sweeperq|LINK
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.
Jul 16, 2007 07:37 PM|Odegaard|LINK
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.
Jul 16, 2007 08:24 PM|Sweeperq|LINK
I'm not sure how you would do that. Best of luck to you.
Jul 17, 2007 10:32 PM|Rex Lin - MSFT|LINK
You can use the following syntax toEncodes a URL string.
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
Jul 18, 2007 11:48 AM|Odegaard|LINK
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,
will be processed by my HttpModule. So if someone typed in any of the following
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:
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.
Jul 18, 2007 10:36 PM|Rex Lin - MSFT|LINK
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 +"
// 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;
Jul 19, 2007 11:36 AM|Odegaard|LINK
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.