views:

1067

answers:

4

Hello,

I would like to get the exact url that user typed into the browser. Of course I could always use something like Request.Url.ToString() but this does not give me what i want in the following situation:

http://www.mysite.com/rss

With the url above what Request.Url.ToString() would give me is:

http://www.mysite.com/rss/Default.aspx

Does anyone know how to accomplish this?

I have already tried:

Request.Url

Request.RawUrl

this.Request.ServerVariables["CACHE_URL"]

this.Request.ServerVariables["HTTP_URL"]

((HttpWorkerRequest)((IServiceProvider)HttpContext.Current).GetService(typeof(HttpWorkerRequest))).GetServerVariable( "CACHE_URL" )

((HttpWorkerRequest)((IServiceProvider)HttpContext.Current).GetService(typeof(HttpWorkerRequest))).GetServerVariable( "HTTP_URL" )

+3  A: 

Edit: You want the HttpWorkerRequest.GetServerVariable() with the key HTTP_URL or CACHE_URL. Note that the behavior differs between IIS 5 and IIS 6 (see documentation of the keys).

In order to be able to access all server variables (in case you get null), directly access the HttpWorkerRequest:

HttpWorkerRequest workerRequest = 
  (HttpWorkerRequest)((IServiceProvider)HttpContext.Current)
  .GetService(typeof(HttpWorkerRequest));
Lucero
That does not work. It still gives me http://www.mysite.com/rss/Default.aspx
Vance Smith
I tried that (Request.ServerVariables["HTTP_URL"]) and I am getting null...
Vance Smith
Not working. Got null :(I tried ((HttpWorkerRequest)((IServiceProvider)HttpContext.Current).GetService(typeof(HttpWorkerRequest))).GetServerVariable( "CACHE_URL" ) and ((HttpWorkerRequest)((IServiceProvider)HttpContext.Current).GetService(typeof(HttpWorkerRequest))).GetServerVariable( "HTTP_URL" )
Vance Smith
What is your platform? Not IIS maybe?
Lucero
I am currently developing on my local workstation (VS 2008 sp 1, .net 3.5 sp1, Win XP, IE7, IIS 5.1)
Vance Smith
It's "URL", not "HTTP_URL", however both cassini and IIS 7 return the full path including file name when the browser requests just a directory. I've not checked it on IIS 6 - which version are you using Lucero?
Zhaph - Ben Duguid
With IIS 5.1, you should be able to get the URL using "HTTP_URL" (or "URL" - are the docs wrong?). @Zhaph - Ben Duguid: I'm referring to the documentation about IIS server variables I've linked to.
Lucero
Hmm, sorry, I stand corrected - I was iterating over the collection. However, HTTP_URL returns the same as URL. The one that seems to work for me on IIS 7 is Request.ServerVariables["CACHE_URL"] however this returns for me: http://www.mysite.com:80/rss/ (i.e. with a port number).
Zhaph - Ben Duguid
However: http://go.microsoft.com/fwlink/?LinkId=52471 states that CACHE_URL isn't available on IIS 5.1 and earlier.
Zhaph - Ben Duguid
A: 
Request.RawUrl

I think is the monkey you are after...

pzycoman
Nope, that was my first suggestion also (I edited it away since then).
Lucero
+2  A: 

Remember too that the "exact URL that the user entered" may never be available at the server. Each link in the chain from fingers to server can slightly modify the request.

For example if I type xheo.com into my browser window, IE will be convert to http://www.xheo.com automatically. Then when the request gets to IIS it says to the browser - you really want the default page at http://www.xheo.com/Default.aspx. So the browser responds by asking for the default page.

Same thing happens with HTTP 30x redirect requests. The server will likely only ever see the final request made by the browser.

Paul Alexander
Understandable. Im not so much concerned about the http:// or www part of what the user entered. My primary goal is to know if they entered in mysite.com/rss or mysite.com/rss/default.aspx
Vance Smith
Default documents (on IIS) are hanlded internally on the server, not via client redirect. This only applies to directory names not ended with a /
Lucero
I was hoping that somewhere the originally requested url was available...Are you saying that you dont think so? IIS has to have had it at some point so that it then knew what default document to serve up?
Vance Smith
I'm not sure if I should believe the docs anymore, but they tell me that up to V5.1 you get that info in the "HTTP_URL" and from V6 onwards in the "CACHE_URL" (since HTTP_URL is already in the processed form here). I guess I'd have to play around with the different versions to come to a conclusion.
Lucero
Can you explain why you need to differentiate between rss/ and rss/default.aspx? Practically speaking they are the same. I'm sure there's a good reason but understanding it might help find a better solution.
Paul Alexander
Generally this sort of request is for SEO purposes: Please only display 1 URL for each page, not two or three - default documents are the bane of an SEO consultant's life apparently.
Zhaph - Ben Duguid
A: 

Try using "Request.Url.OriginalString" Might give you the thing you are looking for.

Vivek Sharma