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

后端 未结 3 523
佛祖请我去吃肉
佛祖请我去吃肉 2021-02-05 10:41

Hi all, I seem to have found a discrepancy when testing ASP.NET applications locally on the built-in web server with Visual Studio 2008 (Cassini).

I\'ve

3条回答
  •  孤城傲影
    2021-02-05 11:42

    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;.

提交回复
热议问题