204

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?

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
  • See this solution: http://stackoverflow.com/questions/567590/asp-net-absolute-path-of-a-url/567632#567632 – Kimball Robinson Sep 04 '10 at 00:34
  • detailed answer here http://stackoverflow.com/questions/593709/how-to-get-the-url-of-the-current-page-in-c-sharp/16693496#16693496 – Learning Apr 26 '15 at 04:12
  • It's funny to see the duplicate links... at the time this was question was asked Stack Overflow was still less than a month old. – Joel Coehoorn Nov 03 '21 at 17:34

10 Answers10

238

Use this:

Request.Url.AbsoluteUri

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

Mikey
  • 2,837
  • 2
  • 18
  • 17
  • 8
    Remember, this will also include the query part (?key=value....) – Marcel Aug 16 '12 at 14:25
  • 5
    This doesn't work if the application is not hosted on the server root but in a directory. If the application is hosted on www.contoso.com/app/ this will return just www.contoso.com – linkerro Oct 03 '12 at 08:58
121

If you want only the scheme and authority part of the request (protocol, host and port) use

Request.Url.GetLeftPart(UriPartial.Authority)
William
  • 8,007
  • 5
  • 39
  • 43
30

I am using

Request.Url.GetLeftPart(UriPartial.Authority) +
        VirtualPathUtility.ToAbsolute("~/")
sth
  • 222,467
  • 53
  • 283
  • 367
Ivan Stefanov
  • 489
  • 6
  • 12
13

I use this in my code in a custom class. Comes in handy for sending out emails like no-reply@example.com "no-reply@" + BaseSiteUrl Works fine on any site.

// get a sites base urll ex: example.com
public static string BaseSiteUrl
{
    get
    {
        HttpContext context = HttpContext.Current;
        string baseUrl = context.Request.Url.Authority + context.Request.ApplicationPath.TrimEnd('/');
        return baseUrl;
    }

}

If you want to use it in codebehind get rid of context.

Prescient
  • 1,051
  • 3
  • 17
  • 42
  • 2
    Note that `Authority` doesn't include the protocol (`http://` or `https://`). Was fine in your instance but might not be for some. Can be obtained through `System.Web.HttpContext.Current.Request.Url.Scheme`, though. – vapcguy Dec 04 '18 at 16:44
8
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.

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
corey
  • 81
  • 1
  • 1
8

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.

Community
  • 1
  • 1
Darren Kopp
  • 76,581
  • 9
  • 79
  • 93
5

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

new Uri(Request.Url,Request.ApplicationPath)

or

Request.Url.GetLeftPart(UriPartial.Authority)+Request.ApplicationPath
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
pub
  • 51
  • 1
  • 1
3

Request.Url.Host

Stephen Wrighton
  • 36,783
  • 6
  • 67
  • 86
2

If you want to include any unique string on the end, similar to example.com?id=99999, then use the following

Dim rawUrl As String = Request.RawUrl.ToString()
Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
Ben Petersen
  • 471
  • 4
  • 15
2

Using a js file you can capture the following, that can be used in the codebehind as well:

<script type="text/javascript">
    alert('Server: ' + window.location.hostname);
    alert('Full path: ' + window.location.href);
    alert('Virtual path: ' + window.location.pathname);
    alert('HTTP path: ' + 
        window.location.href.replace(window.location.pathname, ''));    
</script>
von v.
  • 16,868
  • 4
  • 60
  • 84
REEP
  • 51
  • 3