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?
-
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 Answers

- 2,837
- 2
- 18
- 17
-
8
-
5This 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
If you want only the scheme and authority part of the request (protocol, host and port) use
Request.Url.GetLeftPart(UriPartial.Authority)

- 8,007
- 5
- 39
- 43
-
3An even better solution is posted at http://stackoverflow.com/questions/567590/asp-net-absolute-path-of-a-url – Kimball Robinson Sep 04 '10 at 00:33
-
25@Kimball I'm not sure concatenating strings together is a better solution. – William Mar 27 '11 at 11:10
I am using
Request.Url.GetLeftPart(UriPartial.Authority) +
VirtualPathUtility.ToAbsolute("~/")

- 222,467
- 53
- 283
- 367

- 489
- 6
- 12
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.

- 1,051
- 3
- 17
- 42
-
2Note 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
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.

- 97,193
- 102
- 206
- 364

- 81
- 1
- 1
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.

- 1
- 1

- 76,581
- 9
- 79
- 93
I'm facing same problem and so far I found:
new Uri(Request.Url,Request.ApplicationPath)
or
Request.Url.GetLeftPart(UriPartial.Authority)+Request.ApplicationPath

- 97,193
- 102
- 206
- 364

- 51
- 1
- 1
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()

- 399,467
- 113
- 570
- 794

- 471
- 4
- 15
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>