Marcus L
Marcus L

Reputation: 4078

Request.Url.Host and ApplicationPath in one call

Is there any way to get HttpContext.Current.Request.Url.Host and HttpContext.Current.Request.ApplicationPath in one call?

Something like "full application url"?

EDIT: Clarification - what I need is this the part within []:

http://[www.mysite.com/mywebapp]/Pages/Default.aspx

I ask simply out of curiosity.

EDIT 2: Thanks for all the replies, but none of them were exactly what I was looking for. FYI, I solved the problem this way (but am still interested in knowing if there's a smoother way):

public string GetWebAppRoot()
{
    if(HttpContext.Current.Request.ApplicationPath == "/")
        return "http://" + HttpContext.Current.Request.Url.Host;
    else
        return "http://" + HttpContext.Current.Request.Url.Host + HttpContext.Current.Request.ApplicationPath;
}

Upvotes: 18

Views: 78928

Answers (6)

Raj
Raj

Reputation: 6830

HttpContext.Current.Request.Url.AbsoluteUri

Upvotes: 1

Samuel
Samuel

Reputation: 2019

This was not working on my localhost with a port number so made minor modification:

  private string GetWebAppRoot()
    {
        string host = (HttpContext.Current.Request.Url.IsDefaultPort) ? 
            HttpContext.Current.Request.Url.Host : 
            HttpContext.Current.Request.Url.Authority;
        host = String.Format("{0}://{1}", HttpContext.Current.Request.Url.Scheme, host);            
        if (HttpContext.Current.Request.ApplicationPath == "/")
            return host;
        else
            return host + HttpContext.Current.Request.ApplicationPath;
    }

Upvotes: 18

Dan Diplo
Dan Diplo

Reputation: 25349

What you should really do is:

return String.Format("{0}://{1}/", Request.Url.Scheme, Request.Url.Host);

That way it works if you are using HTTPS (or some other schema!)

Upvotes: 7

Marcus L
Marcus L

Reputation: 4078

Thanks for all the replies, but none of them were exactly what I was looking for. FYI, I solved the problem this way (but am still interested in knowing if there's a smoother way):

public string GetWebAppRoot()
{
    if(HttpContext.Current.Request.ApplicationPath == "/")
        return "http://" + HttpContext.Current.Request.Url.Host;
    else
        return "http://" + HttpContext.Current.Request.Url.Host + HttpContext.Current.Request.ApplicationPath;
}

Upvotes: 3

MartinHN
MartinHN

Reputation: 19792

public static string GetSiteRoot()
{
  string port = System.Web.HttpContext.Current.Request.ServerVariables["SERVER_PORT"];
  if (port == null || port == "80" || port == "443")
    port = "";
  else
    port = ":" + port;

  string protocol = System.Web.HttpContext.Current.Request.ServerVariables["SERVER_PORT_SECURE"];
  if (protocol == null || protocol == "0")
    protocol = "http://";
  else
    protocol = "https://";

  string sOut = protocol + System.Web.HttpContext.Current.Request.ServerVariables["SERVER_NAME"] + port + System.Web.HttpContext.Current.Request.ApplicationPath;

  if (sOut.EndsWith("/"))
  {
    sOut = sOut.Substring(0, sOut.Length - 1);
  }

  return sOut;
}

Upvotes: 32

veggerby
veggerby

Reputation: 9020

Check this post:

public static Uri GetBaseUrl(HttpRequest request)
{
    Uri contextUri = new Uri(request.Url, request.RawUrl);
    UriBuilder realmUri = new UriBuilder(contextUri) { Path = request.ApplicationPath, Query = null, Fragment = null };
    return realmUri.Uri;
}

public static string GetAbsoluteUrl(HttpRequest request, string relativeUrl)
{
    return new Uri(GetBaseUrl(request), VirtualPathUtility.ToAbsolute(relativeUrl)).AbsoluteUri;
}

If you don't get what you need from GetBaseUrl direcly, is should be possible to do:

GetAbsoluteUrl(HttpContext.Current.Request, "/")

Upvotes: 2

Related Questions