Request.Url.Host and ApplicationPath in one call
Asked Answered
S

6

18

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;
}
Statistician answered 27/3, 2009 at 13:1 Comment(1)
You want Request.Url.Authority, in theory; Host doesn't have the port. Unfortunately, your best bet is to use what's in Request.Headers["HOST"], as it is supposed to contain the dns:port of the server that the client used to connect. If you're worried about HTTP/1.0 requests, you can fall-back to Request.Url.Host, of course. I have personally experienced Request.Url.Authority to return the port that the server software is bound to, not the port in the "HOST" header---which is what the client used to connect. (FWIW, it was in SSRS 2012's Report Manager web site.)Ionone
P
32
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;
}
Party answered 27/3, 2009 at 14:6 Comment(4)
IIRC SERVER_NAME might not get you the hostname you need - e.g. host-header resolution (msdn.microsoft.com/en-us/library/ms524602.aspx)! Correct me if I'm wrong :)Wards
I've used this one for years, on different sites - and it gives you the server name of the request made, which is all from http:// to the first /Party
If this works, thanks a bunch. Was looking for something that would work with non-standard ports and with urls like blah.com/web/test/hello.aspx. I'll give it a spin.Tribade
I removed the Answer-tag from my own post, and moved it to this as this answer covers more circumstances.Statistician
W
18

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;
    }
Wrapping answered 28/9, 2009 at 1:53 Comment(0)
S
7

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

Stifling answered 19/8, 2009 at 13:46 Comment(0)
S
3

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;
}
Statistician answered 30/3, 2009 at 16:6 Comment(1)
Yes, my solution doesn't really handle that, but this would: return String.Format("{0}://{1}/", Request.Url.Scheme, Request.Url.Host);Statistician
W
2

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, "/")

Wards answered 27/3, 2009 at 13:10 Comment(0)
A
1
HttpContext.Current.Request.Url.AbsoluteUri
Alarcon answered 27/3, 2009 at 13:11 Comment(1)
Thanks, but no - that gives you the full path of the ASPX-page it's called from. I only want host + application path, not page path.Statistician

© 2022 - 2024 — McMap. All rights reserved.