Get URL of ASP.Net Page in code-behind

asp.net

asp.net Problem Overview


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?

asp.net Solutions


Solution 1 - asp.net

Use this:

Request.Url.AbsoluteUri

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

Solution 2 - asp.net

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

Request.Url.GetLeftPart(UriPartial.Authority)

Solution 3 - asp.net

I am using

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

Solution 4 - asp.net

I use this in my code in a custom class. Comes in handy for sending out emails like [email protected] "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.

Solution 5 - asp.net

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.

Solution 6 - asp.net

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.

Solution 7 - asp.net

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

new Uri(Request.Url,Request.ApplicationPath)

or

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

Solution 8 - asp.net

Request.Url.Host

Solution 9 - asp.net

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

Solution 10 - asp.net

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>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJoel CoehoornView Question on Stackoverflow
Solution 1 - asp.netMikeyView Answer on Stackoverflow
Solution 2 - asp.netWilliamView Answer on Stackoverflow
Solution 3 - asp.netIvan StefanovView Answer on Stackoverflow
Solution 4 - asp.netPrescientView Answer on Stackoverflow
Solution 5 - asp.netDarren KoppView Answer on Stackoverflow
Solution 6 - asp.netcoreyView Answer on Stackoverflow
Solution 7 - asp.netpubView Answer on Stackoverflow
Solution 8 - asp.netStephen WrightonView Answer on Stackoverflow
Solution 9 - asp.netBen PetersenView Answer on Stackoverflow
Solution 10 - asp.netREEPView Answer on Stackoverflow