tags:

views:

29869

answers:

8

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?

+2  A: 

Request.Url.Host

Stephen Wrighton
+29  A: 

Use this:

Request.Url.AbsoluteUri

That will get you the full path (including http://...)

Mikey
+3  A: 

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.

Darren Kopp
+14  A: 

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 :)

WDuffy
Not having this line of code has bugged me for years... Thanks!
Christopher Edwards
An even better solution is posted at http://stackoverflow.com/questions/567590/asp-net-absolute-path-of-a-url
Kimball Robinson
+2  A: 

I'm facing same problem and so far I found:

new Uri(Request.Url,Request.ApplicationPath)

or

Request.Url.GetLeftPart(UriPartial.Authority)+Request.ApplicationPath

pub
+1  A: 

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.

corey
A: 

When you want to only HOst Name with Http://

Request.Url.GetLeftPart(UriPartial.Authority)

sudhir
This is already in the second highest answer.
ck
A: 

I am using

Request.Url.GetLeftPart(UriPartial.Authority) +
        VirtualPathUtility.ToAbsolute("~/")
Ivan Stefanov