How to get application base url while behind reverse proxy

Viewed 3311

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?

When requesting this url via a gateway:

http://conoso.com/behind/some/reverseproxy/api/values/

The request is being re-routed by the gateway to some other location:

http://10.0.0.0/api/values

It is accessing the following ApiController ValuesController's Get method using the DefaultApi route:

// GET api/values
public HttpResponseMessage Get()
{
  var res = Request.CreateResponse();
  res.Headers.Add(
    "Location",
    Url.Link(
      "DefaultApi",
      new
        {
          id = 1
        }));
  return res;
}

It returns the following header value:

Location: http://10.0.0.0/api/values/1

while I was hoping to be sending the valid path of:

Location: http://conoso.com/behind/some/reverseproxy/api/values/1

The alternative to using the built in methods in the framework is to manually format the string:

var baseUrl = "http://conoso.com/behind/some/reverseproxy";
string.Format("{0}/values/1", baseUrl);

But that gives off some wicked code smell. Is there a cleaner approach anyone can suggest?

1 Answers
Related