Why does Request["host"] == "dev.testhost.com:1234" whereas Request.Url.Host == "localhost" Why does Request["host"] == "dev.testhost.com:1234" whereas Request.Url.Host == "localhost" asp.net asp.net

Why does Request["host"] == "dev.testhost.com:1234" whereas Request.Url.Host == "localhost"


Request.Headers["host"] is the value received from the application that connects to the server, while the other value is the one the server gets when it tries to get the domain name.

The browser uses in the request the domain name entered because that is used in the case of virtual domains. The server reports the one set in the server preferences, or the first one it finds.

EDIT: Looking at the code of Cassini to see if it uses some particular settings, I noticed the following code:

public string RootUrl {  get {    if (_port != 80) {      return "http://localhost:" + _port + _virtualPath;    }    else {      return "http://localhost" + _virtualPath;    }  }}//// Socket listening//public void Start() {  try {    _socket = CreateSocketBindAndListen(AddressFamily.InterNetwork, IPAddress.Loopback, _port);  }  catch {    _socket = CreateSocketBindAndListen(AddressFamily.InterNetworkV6, IPAddress.IPv6Loopback, _port);  }  // …}

The explanation seems to be that Cassini makes explicit reference to localhost, and doesn't try to make a reverse DNS lookup. Differently, it would not use return "http://localhost" + _virtualPath;.


The Request.Headers["host"] is the host as specified in the http header from the browser. (e.g. this is what you'd see if you examined the traffic with Fiddler or HttpWatch)

However, ASP.NET loasds this (and other request info) into a System.Uri instance, which parses the request string into its constituent parts. In this case, "Host" refers to literally the host machine part of the original request (e.g. with the tcp port being in the Port) property.

This System.Uri class is a very useful helper class that takes all the pain out of splitting your request into it's parts, whereas the "Host:" (and for that matter the "GET") from the http header are just raw request data.

Although they both have the same name, they are not meant to be the same thing.


It's a matter of what the w3 specs say versus what the Microsoft Uri.Host property is supposed to contain. The naming does not imply an attempt by MS to provide identical functionality. The function that does include port numbers is Uri.Authority.

With the update you posted, you're still facing the same problem, just examining a different aspect of it. The Uri.Host property is not explicity or implicity stated to perform the same function as the headers that are defined in the w3 specs. In long form, here are some quotes from the Uri.Host MSDN page:

Uri.Host Property
Gets the host component of this instance.

Property Value

Type: System.String

A String that contains the host name. This is usually the DNS host name or IP address of the server.

There's no guarantee that this will match what is in the headers, just that it represents the host name in some form.