title: How do I get the site root URL?
body: <p>I want to get the absolute root Url of an ASP.NET application dynamically. This needs to be the full root url to the application in the form: http(s)://hostname(:port)/</p>
  
  <p>I have been using this static method:</p>
  
  <pre><code>public static string GetSiteRootUrl()
  {
      string protocol;
  
      if (HttpContext.Current.Request.IsSecureConnection)
          protocol = "https";
      else
          protocol = "http";
  
      StringBuilder uri = new StringBuilder(protocol + "://");
  
      string hostname = HttpContext.Current.Request.Url.Host;
  
      uri.Append(hostname);
  
      int port = HttpContext.Current.Request.Url.Port;
  
      if (port != 80 &amp;&amp; port != 443)
      {
          uri.Append(":");
          uri.Append(port.ToString());
      }
  
      return uri.ToString();
  }
  </code></pre>
  
  <p>BUT, what if I don't have <code>HttpContext.Current</code> in scope?
  I have encountered this situation in a <code>CacheItemRemovedCallback</code>.</p>