I have an ASP.Net page that will be hosted on a couple different servers, and I want to get the URL of the page (or even better: the site where the page is hosted) as a string for use in the code-behind. Any ideas?
Use this:
Request.Url.AbsoluteUri
That will get you the full path (including http://...)
Do you want the server name? Or the host name?
Request.Url.Host ala Stephen
Dns.GetHostName - Server name
Request.Url will have access to most everything you'll need to know about the page being requested.
Late answer but incase anyone else finds this post it may be useful.
If you want only the scheme and authority part of the request (protocol, host and port) use
Request.Url.GetLeftPart(UriPartial.Authority)
This will give you what Jared's sample code will give you, in one line :)
I'm facing same problem and so far I found:
new Uri(Request.Url,Request.ApplicationPath)
or
Request.Url.GetLeftPart(UriPartial.Authority)+Request.ApplicationPath
Request.Url.GetLeftPart(UriPartial.Authority) + Request.FilePath + "?theme=blue";
that will give you the full path to the page you are sitting on. I added in the querystring.
When you want to only HOst Name with Http://
Request.Url.GetLeftPart(UriPartial.Authority)
I am using
Request.Url.GetLeftPart(UriPartial.Authority) +
VirtualPathUtility.ToAbsolute("~/")