title: How to get application base url while behind reverse proxy
body: <p>Is there any way for an ASP.NET application to be able to derive its url paths and hostname knowing its own routes within the context of a request going through reverse proxy/gateway?</p>
  
  <p>When requesting this url via a gateway:</p>
  
  <pre><code>http://conoso.com/behind/some/reverseproxy/api/values/
  </code></pre>
  
  <p>The request is being re-routed by the gateway to some other location:</p>
  
  <pre><code>http://10.0.0.0/api/values
  </code></pre>
  
  <p>It is accessing the following ApiController ValuesController's Get method using the DefaultApi route:</p>
  
  <pre><code>// GET api/values
  public HttpResponseMessage Get()
  {
    var res = Request.CreateResponse();
    res.Headers.Add(
      "Location",
      Url.Link(
        "DefaultApi",
        new
          {
            id = 1
          }));
    return res;
  }
  </code></pre>
  
  <p>It returns the following header value:</p>
  
  <pre><code>Location: http://10.0.0.0/api/values/1
  </code></pre>
  
  <p>while I was hoping to be sending the valid path of:</p>
  
  <pre><code>Location: http://conoso.com/behind/some/reverseproxy/api/values/1
  </code></pre>
  
  <p>The alternative to using the built in methods in the framework is to manually format the string:</p>
  
  <pre><code>var baseUrl = "http://conoso.com/behind/some/reverseproxy";
  string.Format("{0}/values/1", baseUrl);
  </code></pre>
  
  <p>But that gives off some wicked code smell. Is there a cleaner approach anyone can suggest?</p>