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

后端 未结 3 524
佛祖请我去吃肉
佛祖请我去吃肉 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

    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.

提交回复
热议问题