Search code examples
asp.netiisconfigurationhost

Why do I need to edit the hosts file to make my ASP.NET projects work properly with IIS?


This is the way I have been taught:

  1. Add the website IIS and point to the ASP.NET files.
  2. Add the host name within IIS and an example of this would be:

    myAspProject.local.co.uk

  3. Add the same host name in the host file and an example of this would be:

    127.0.0.1 myAspProject.local.co.uk

Why do I need to edit this host file like I have above? Could you please explain the story from the point the user enters the URL in the browser.


Solution

  • What you are looking at here is called 'Domain Name Resolution'. It can be a complicated subject but a simplified explination will show you what you are doing here.

    When you type a name into your browser you are asking you browser to retrieve that page. Now let's assume you typed in 'www.stackoverflow'com'. Your computer cannot connect to www.stackoverflow.com because computers talk using number, not letters; in this case the number we are talking about is the 'IP Address'. So what you need is the IP addresss associated with 'www.stackoverflow'com'.

    The way your computer will do this is it will reach out to a Domain Name Server (DNS) and ask "what IP Address is associated with this name 'www.stackoverflow.com' ?". The DNS will return the IP Address of Stack Overflow and then your computer will then talk to the server on that address.

    Now, before your computer actually talks to the DNS it does a couple of other things, it first checks its local cache to see if it has already asked the DNS this question and already got a response, then it will check the hosts file (this is what you are asking about) to see if there is an IP Address associated with the name on your computer.

    In your case you do have an IP Address associated with the name, you have associated 127.0.0.1 which is a special IP Address which tells the computer "look inside yourself" as this is a local IP address. Anyone else trying the same IP address will look inside their own computer, not yours.

    Does that answer it enough for you?