Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get URL of ASP.Net Page in code-behind [duplicate]

Tags:

asp.net

Use this:

Request.Url.AbsoluteUri

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


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

Request.Url.GetLeftPart(UriPartial.Authority)

I am using

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

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.


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.


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.