rjarmstrong
rjarmstrong

Reputation: 1231

What is the quickest way to get the absolute uri for the root of the app in asp.net?

What is the simplest way to get: http://www.[Domain].com in asp.net?

There doesn't seem to be one method which can do this, the only way I know is to do some string acrobatics on server variables or Request.Url. Anyone?

Upvotes: 6

Views: 3984

Answers (10)

Jakob Dyrby
Jakob Dyrby

Reputation: 162

I use this property on Page to handle cases virtual directories and default ports:

string FullApplicationPath {
    get {
        StringBuilder sb = new StringBuilder();
        sb.AppendFormat("{0}://{1}", Request.Url.Scheme, Request.Url.Host);

        if (!Request.Url.IsDefaultPort)
            sb.AppendFormat(":{0}", Request.Url.Port);

        if (!string.Equals("/", Request.ApplicationPath))
            sb.Append(Request.ApplicationPath);

        return sb.ToString();
    }
}

Upvotes: 1

Cyril Durand
Cyril Durand

Reputation: 16192

We can use Uri and his baseUri constructor :

  • new Uri(this.Request.Url, "/") for the root of the website
  • new Uri(this.Request.Url, this.Request.ResolveUrl("~/")) for the root of the website

Upvotes: 3

Oliver Crow
Oliver Crow

Reputation: 376

Combining the best of what I've seen on this question so far, this one takes care of:

  1. http and https
  2. standard ports (80, 443) and non standard
  3. application hosted in a sub-folder of the root

    string url = String.Format(
        Request.Url.IsDefaultPort ? "{0}://{1}{3}" : "{0}://{1}:{2}{3}",
        Request.Url.Scheme, Request.Url.Host,
        Request.Url.Port, ResolveUrl("~/"));
    

Upvotes: 0

empz
empz

Reputation: 11788

I had to deal with something similar, I needed a way to programatically set the tag to point to my website root.

The accepted solution wasn't working for me because of localhost and virtual directories stuff.

So I came up with the following solution, it works on localhost with or without virtual directories and of course under IIS Websites.

string.Format("{0}://{1}:{2}{3}", Request.Url.Scheme, Request.Url.Host, Request.Url.Port, ResolveUrl("~")

Upvotes: 0

Dhaust
Dhaust

Reputation: 5550

This method handles http/https, port numbers and query strings.

'Returns current page URL 
Function fullurl() As String
    Dim strProtocol, strHost, strPort, strurl, strQueryString As String
    strProtocol = Request.ServerVariables("HTTPS")
    strPort = Request.ServerVariables("SERVER_PORT")
    strHost = Request.ServerVariables("SERVER_NAME")
    strurl = Request.ServerVariables("url")
    strQueryString = Request.ServerVariables("QUERY_STRING")

    If strProtocol = "off" Then
        strProtocol = "http://"
    Else
        strProtocol = "https://"
    End If

    If strPort <> "80" Then
        strPort = ":" & strPort
    Else
        strPort = ""
    End If

    If strQueryString.Length > 0 Then
        strQueryString = "?" & strQueryString
    End If

    Return strProtocol & strHost & strPort & strurl & strQueryString
End Function

Upvotes: 0

stephenbayer
stephenbayer

Reputation: 12431

I really like the way CMS handled this question the best, using the String.Format, and the Page.Request variables. I'd just like to tweak it slightly. I just tested it on one of my pages, so, i'll copy the code here:

String baseURL = string.Format(
   (Request.Url.Port != 80) ? "{0}://{1}:{2}" : "{0}://{1}", 
    Request.Url.Scheme, 
    Request.Url.Host, 
    Request.Url.Port)

Upvotes: 2

Mitchel Sellers
Mitchel Sellers

Reputation: 63126

You can use something like this.

System.Web.HttpContext.Current.Server.ResolveUrl("~/")

It maps to the root of the application. now if you are inside of a virtual directory you will need to do a bit more work.

Edit

Old posting contained incorrect method call!

Upvotes: 2

Christian C. Salvad&#243;
Christian C. Salvad&#243;

Reputation: 827366

You can do it like this:

string.Format("{0}://{1}:{2}", Request.Url.Scheme, Request.Url.Host, Request.Url.Port)

And you'll get the generic URI syntax <protocol>://<host>:<port>

Upvotes: 2

Steven A. Lowe
Steven A. Lowe

Reputation: 61233

this.Request.Url.Host

Upvotes: 1

SaaS Developer
SaaS Developer

Reputation: 9895

System.Web.UI.Page.Request.Url

Upvotes: 1

Related Questions