To test a website hosted on localhost at a specific port, you must map a custom domain name to the IP address 127.0.0.1 by editing the system hosts file. This bypasses DNS resolution and directs your browser to the local IIS instance, allowing you to access the site via http://yourdomain.com:PORT instead of the raw IP and port combination.
Understanding the Hosts File Mechanism
The
hosts file is a plain text file used by operating systems to translate hostnames into IP addresses before querying external DNS servers. By adding an entry here, you create a local override that tells Windows to route traffic for a specific domain directly to your machine's loopback adapter. This technique is essential when testing web applications bound to non-standard ports or when you need a clean URL structure without exposing the port number in the address bar during development.
In Windows Server environments like 2012 and 2019, as well as client OS versions paired with Visual Studio 2022, this file resides in the
C:\Windows\System32\drivers\etc directory. It typically contains no extension and may be named just
hosts. Because this file controls critical network routing, modifying it requires administrative privileges. Without elevated permissions, any changes will be rejected by the operating system to prevent unauthorized network manipulation.
Editing the Hosts File on Windows Server
To begin the configuration process, you must first locate the correct file path. Navigate to
C:\Windows\System32\drivers\etc using File Explorer. You will see a file named
hosts which might have a hidden extension depending on your view settings. Before making any edits, it is prudent to create a backup copy of this file. Rename the original to
hosts.bak so you can restore the previous state if you encounter connectivity issues later.
Next, open a command prompt or PowerShell window as an administrator. Right-click the Start button or search for "Command Prompt" in the taskbar, then select "Run as administrator." Alternatively, you can launch Notepad as an administrator by right-clicking the Notepad icon and selecting the same option. Once the elevated editor is open, use the File menu to open the
hosts file located in the
etc folder. If you opened Command Prompt, you can use the command
notepad C:\Windows\System32\drivers\etc\hosts to achieve the same result within the admin context.
Inside the file, scroll to the bottom and add a new line. The format is straightforward: the IP address followed by a space or tab, then the domain name you wish to use. For a localhost test on a specific port, the entry should look like this:
127.0.0.1 mytestsite.local
After saving the file, close the editor. The changes are applied immediately; however, some browsers cache DNS lookups aggressively. To ensure your browser recognizes the new mapping, clear the DNS cache or restart the browser entirely. On Windows Server, you can also flush the DNS resolver cache by running
ipconfig /flushdns in the administrator command prompt.
Configuring IIS and Visual Studio Integration
With the hosts file updated, you must ensure that Internet Information Services (IIS) is configured to listen for requests directed at your new domain name. In IIS Manager, navigate to the Sites section and select your target website. Open the "Bindings" dialog from the Actions pane on the right. Here, you will see existing bindings, likely pointing to
*:80: or
localhost.
Add a new binding for HTTP. Set the Type to
http, the Port to your desired port (e.g., 8080), and crucially, set the Host name to
mytestsite.local. This step binds the IIS listener specifically to the hostname defined in your hosts file. If you are using Visual Studio 2022 to develop the application, ensure that the project properties match this configuration. In the Debug settings, verify that the server URL corresponds to the new hostname and port.
This setup allows you to access the site using
http://mytestsite.local:8080. If you prefer not to include the port in the URL, you would need to bind the site to port 80 and configure IIS to redirect or handle the traffic accordingly, but keeping the explicit port is safer for avoiding conflicts with other services. Remember that if you change the binding in IIS, you may need to restart the IIS service using
iisreset in the administrator command prompt to apply the changes fully.
For more detailed guidance on hosting websites locally, refer to our guide on
How to Host My Website on My Local PC: Step-by-Step Guide. Additionally, if you encounter issues where the server appears offline despite correct configurations, check our article on
IIS Server Redirects to Offline Server: How to Fix Error Code 500302.
Troubleshooting Common Issues
If you cannot reach the site after these steps, several factors could be interfering. First, verify that the Windows Firewall is not blocking inbound connections on the specific port. Go to "Windows Defender Firewall with Advanced Security" and create an inbound rule allowing TCP traffic on your chosen port. Second, ensure that the IIS site status is started. A stopped site will not respond regardless of DNS configuration.
Browser caching is another frequent culprit. Modern browsers store DNS records for a period known as Time to Live (TTL). Even though the hosts file overrides DNS, some browsers may still hold stale connection data. Clearing the browser cache and cookies often resolves apparent connectivity failures. Finally, check for typos in the hosts file. The domain name must match exactly what you type in the browser address bar, including case sensitivity in some contexts, though Windows is generally case-insensitive for hostnames.
Frequently Asked Questions
Do I need to restart IIS after changing the hosts file?
No, restarting IIS is not required because the hosts file affects the client-side DNS resolution, not the server-side listening sockets. However, you must restart the IIS site or run
iisreset if you modify the IIS bindings to include the new hostname.
Why does my browser still show "Connection Refused"?
This error usually indicates that the firewall is blocking the port or the IIS site is not listening on that specific port. Verify your inbound firewall rules allow TCP traffic for the port and ensure the IIS binding includes the correct host name and port combination.
Can I use localhost instead of a custom domain name?
You can use
localhost, but it often defaults to port 80 or 443 unless explicitly specified. Using a custom domain like
mytestsite.local allows you to simulate production environments more accurately and avoids conflicts with other services that might be bound to
localhost on standard ports.
Does editing the hosts file affect other users on the server?
Yes, the hosts file is a system-wide configuration. Any user account on the Windows Server instance will see the same mappings. Ensure that the changes do not conflict with other internal services or domains that rely on different IP resolutions for those hostnames.
How do I revert the changes if something breaks?
Simply delete the line you added to the hosts file or rename the backup file back to
hosts. After restoring the original file, run
ipconfig /flushdns to clear the cached entries and ensure your system returns to normal DNS behavior immediately.