Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the exact url the user typed into the browser

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")
like image 244
Vance Smith Avatar asked Apr 16 '09 19:04

Vance Smith


2 Answers

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)); 
like image 77
Lucero Avatar answered Sep 26 '22 13:09

Lucero


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.

like image 40
Paul Alexander Avatar answered Sep 25 '22 13:09

Paul Alexander