Testing CSK sites locally with domain names
Have you ever needed to test a site using the site's actual domain name, before being able to actually point that domain name to the server on which you're developing the site? As most of you probably already know, testing a site using http://localhost/mysitename/ in many cases is less than ideal, and isn't even possible in some cases.
One such case is when using the ASP.NET Community Starter Kit, which allows you to host multiple community sites using the same codebase, by resolving the requested domain and retrieving the specific content for that community, with that content stored in a database. Trouble is, the CSK isn't really set up for resolving sites based on subdirectories of a given server address, so testing a site before deploying it and pointing the DNS records for the domain at that site is a little tricky.
I ran into this recently when building a new Web server to run my CSK-based sites (and in the process finally upgrading to the 1.0 version of the CSK). I scratched my head for a little bit on how to test the new version of the sites, while still leaving the DNS pointing at the live sites, when I remembered the hosts file. The hosts file, which resides at %windir%\system32\drivers\etc\hosts, allows you to manually map domain names to IP addresses. The default hosts file looks like the following:
# Copyright (c) 1993-1999 Microsoft Corp. # # This is a sample HOSTS file used by Microsoft TCP/IP for Windows. # # This file contains the mappings of IP addresses to host names. Each # entry should be kept on an individual line. The IP address should # be placed in the first column followed by the corresponding host name. # The IP address and the host name should be separated by at least one # space. # # Additionally, comments (such as these) may be inserted on individual # lines or following the machine name denoted by a '#' symbol. # # For example: # # 102.54.94.97 rhino.acme.com # source server # 38.25.63.10 x.acme.com # x client host 127.0.0.1 localhost
The advantage here is that the mappings in the hosts file take precedence over the records retrieved from DNS, so if you map your desired domain name to your local IP address, any requests from that machine to the domain will go to the test machine, not the server configured in DNS:
192.168.1.125 mysitename.com 192.168.1.125 www.mysitename.comVoila! Local testing without messing up existing live sites. Note that you can use the # character to comment out a line, so you can leave your mappings in the hosts file for future use, if desired.