How can I get the root domain URI in ASP.NET?

asp.netUrl

asp.net Problem Overview


Let's say I'm hosting a website at http://www.foobar.com.

Is there a way I can programmatically ascertain "http://www.foobar.com/" in my code behind (i.e. without having to hardcode it in my web config)?

asp.net Solutions


Solution 1 - asp.net

string baseUrl = Request.Url.GetLeftPart(UriPartial.Authority);

Uri::GetLeftPart Method:

> The GetLeftPart method returns a string containing the leftmost portion of the URI string, ending with the portion specified by part.

UriPartial Enumeration:

> The scheme and authority segments of the URI.

Solution 2 - asp.net

For anyone still wondering, a more complete answer is available at http://devio.wordpress.com/2009/10/19/get-absolut-url-of-asp-net-application/.

public string FullyQualifiedApplicationPath
{
    get
    {
        //Return variable declaration
        var appPath = string.Empty;

        //Getting the current context of HTTP request
        var context = HttpContext.Current;

        //Checking the current context content
        if (context != null)
        {
            //Formatting the fully qualified website url/name
            appPath = string.Format("{0}://{1}{2}{3}",
                                    context.Request.Url.Scheme,
                                    context.Request.Url.Host,
                                    context.Request.Url.Port == 80
                                        ? string.Empty
                                        : ":" + context.Request.Url.Port,
                                    context.Request.ApplicationPath);
        }

        if (!appPath.EndsWith("/"))
            appPath += "/";

        return appPath;
    }
}

Solution 3 - asp.net

HttpContext.Current.Request.Url can get you all the info on the URL. And can break down the url into its fragments.

Solution 4 - asp.net

If example Url is http://www.foobar.com/Page1

HttpContext.Current.Request.Url; //returns "http://www.foobar.com/Page1"


HttpContext.Current.Request.Url.Host; //returns "www.foobar.com"


HttpContext.Current.Request.Url.Scheme; //returns "http/https"


HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority); //returns "http://www.foobar.com"

Solution 5 - asp.net

string hostUrl = Request.Url.Scheme + "://" + Request.Url.Host; //should be "http://hostnamehere.com"

Solution 6 - asp.net

To get the entire request URL string:

HttpContext.Current.Request.Url

To get the www.foo.com portion of the request:

HttpContext.Current.Request.Url.Host

Note that you are, to some degree, at the mercy of factors outside your ASP.NET application. If IIS is configured to accept multiple or any host header for your application, then any of those domains which resolved to your application via DNS may show up as the Request Url, depending on which one the user entered.

Solution 7 - asp.net

Match match = Regex.Match(host, "([^.]+\\.[^.]{1,3}(\\.[^.]{1,3})?)$");
string domain = match.Groups[1].Success ? match.Groups[1].Value : null;

host.com => return host.com
s.host.com => return host.com

host.co.uk => return host.co.uk
www.host.co.uk => return host.co.uk
s1.www.host.co.uk => return host.co.uk

Solution 8 - asp.net

--Adding the port can help when running IIS Express

Request.Url.Scheme + "://" + Request.Url.Host + ":" + Request.Url.Port

Solution 9 - asp.net

string domainName = Request.Url.Host

Solution 10 - asp.net

I know this is older but the correct way to do this now is

string Domain = HttpContext.Current.Request.Url.Authority

That will get the DNS or ip address with port for a server.

Solution 11 - asp.net

This works also:

> string url = HttpContext.Request.Url.Authority;

Solution 12 - asp.net

C# Example Below:

string scheme = "http://";
string rootUrl = default(string);
if (Request.ServerVariables["HTTPS"].ToString().ToLower() == "on")
{
  scheme = "https://";
}
rootUrl = scheme + Request.ServerVariables["SERVER_NAME"].ToString();

Solution 13 - asp.net

string host = Request.Url.Host;
Regex domainReg = new Regex("([^.]+\\.[^.]+)$");
HttpCookie cookie = new HttpCookie(cookieName, "true");
if (domainReg.IsMatch(host))
{
  cookieDomain = domainReg.Match(host).Groups[1].Value;                                
}

Solution 14 - asp.net

This will return specifically what you are asking.

Dim mySiteUrl = Request.Url.Host.ToString()

I know this is an older question. But I needed the same simple answer and this returns exactly what is asked (without the http://).

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
QuestioncoreView Question on Stackoverflow
Solution 1 - asp.netGeorgeView Answer on Stackoverflow
Solution 2 - asp.netBrian HasdenView Answer on Stackoverflow
Solution 3 - asp.netJoshBerkeView Answer on Stackoverflow
Solution 4 - asp.netDheeraj PalagiriView Answer on Stackoverflow
Solution 5 - asp.netBen LeshView Answer on Stackoverflow
Solution 6 - asp.netRex MView Answer on Stackoverflow
Solution 7 - asp.netNQuenaultView Answer on Stackoverflow
Solution 8 - asp.netJonathanCView Answer on Stackoverflow
Solution 9 - asp.netSk93View Answer on Stackoverflow
Solution 10 - asp.netidlehands23View Answer on Stackoverflow
Solution 11 - asp.netFribu - Smart SolutionsView Answer on Stackoverflow
Solution 12 - asp.netSaul DolginView Answer on Stackoverflow
Solution 13 - asp.netShawn ConstanceView Answer on Stackoverflow
Solution 14 - asp.nettomepennView Answer on Stackoverflow