8

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")
jor
  • 2,058
  • 2
  • 26
  • 46
BeYourOwnGod
  • 2,345
  • 7
  • 30
  • 35
  • If you look at the Cassini source code, you will see that the URL requested by the user is overwritten (in certain cases) before HttpRuntime.ProcessRequest is ever called. That pretty much rules out any HttpWorkerRequest-agnostic way of doing this. – Michael Kropat Nov 29 '10 at 22:34

6 Answers6

6

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
  • 59,176
  • 9
  • 122
  • 152
  • 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" ) – BeYourOwnGod Apr 16 '09 at 20:38
  • What is your platform? Not IIS maybe? – Lucero Apr 16 '09 at 20:52
  • I am currently developing on my local workstation (VS 2008 sp 1, .net 3.5 sp1, Win XP, IE7, IIS 5.1) – BeYourOwnGod Apr 16 '09 at 21:00
  • 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 Apr 16 '09 at 21:07
  • 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 Apr 16 '09 at 21:14
  • 1
    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 Apr 16 '09 at 21:39
  • However: http://go.microsoft.com/fwlink/?LinkId=52471 states that CACHE_URL isn't available on IIS 5.1 and earlier. – Zhaph - Ben Duguid Apr 16 '09 at 21:41
4

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
  • 31,970
  • 14
  • 96
  • 151
  • 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 – BeYourOwnGod Apr 16 '09 at 21:02
  • 1
    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 Apr 16 '09 at 21:03
  • 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? – BeYourOwnGod Apr 16 '09 at 21:14
  • 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 Apr 16 '09 at 21:17
  • 2
    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 Apr 16 '09 at 21:40
  • 1
    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 Apr 16 '09 at 21:44
3

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

1

It is possible, you just need to combining a few of the values from the request object to rebuild the exact url entered:

Dim pageUrl As String = String.Format("{0}://{1}{2}", 
    Request.Url.Scheme, 
    Request.Url.Host, 
    Request.RawUrl)

Response.Write(pageUrl)

Entering the address http://yousite.com/?hello returns exactly:

http://yousite.com/?hello
George Filippakos
  • 16,359
  • 15
  • 81
  • 92
0

Easiest way to do this is used client-side programming to extract the exact url:

<script language="javascript" type="text/javascript"> 
document.write (document.location.href); 
</script>
StacMan
  • 216
  • 1
  • 3
  • 8
0
Request.RawUrl

I think is the monkey you are after...

Surgical Coder
  • 1,086
  • 1
  • 16
  • 25