[开发故事]第五回,用想要的域名运行你的本地Web应用

anytao.net | 《你必须知道的.NET》网站 | Anytao技术博客 

发布日期:2009.11.27 作者:Anytao
© 2009 Anytao.com ,Anytao原创作品,转贴请注明作者和出处。

 

作为Web开发者,在本地部署应用,我们都对localhost有无限的熟悉,也有无限的无奈。不管什么情况下,不同的应用都被冠以localhost:XXXX的地址,好像有性格的Web应该,都只有张三、李四这样的称呼。很多情况下,我们可能想有更个性的url来代表不同的应用,那么没有域名或者不想劳神DNS解析的情况下,让部署于本地的应该,有一个好听的url是不错的想法。那么,我们就不妨来试试。

主要的工作包括两个基本的简单步骤:

  • 配置本机Host
  • 在IIS中配置WebSite

配置本机的Host服务

默认情况下,Host文件位于%systemroot%\system32\drivers\etc\ 目录下, 并且只能以管理员身份进行编辑。以“Run as Administrator“打开“Notepad“,并到指定的目录打开Host文件,如下:

# Copyright (c) 1993-2009 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

# localhost name resolution is handled within DNS itself.

#     127.0.0.1       localhost

#        ::1             localhost

特别提醒,在修改Host文件前,最好对原文件进行一下备份。

然后我们就可以配置想要的域名,例如:

#     127.0.0.1       localhost

#     ::1                localhost

       127.0.0.1       dev.anytao.net

       127.0.0.2       prj.anytao.net

       127.0.0.3       lab.anytao.net

上述配置都是有效的,这就意味着在本机环境下,通过lab.anytao.net就可以访问127.0.0.3的IP地址,通过localhost或者dev.anytao.net均可访问127.0.0.1地址,不过通常情况下,127.0.0.1被默认为localhost,所以,我们应该尽量避免这种配置。

在IIS中配置你的WebSite

有了可用的域名,那么剩下的工作就比较熟悉了。我们仅仅简单的回忆一下,通常在本机进行应用部署时,我们在添加应用时通过设置不同的端口来区分不同的应该,例如:

现在,一切变得很简单,你只需要在IP绑定时指定你在Host中配置的IP即可。例如如果我想发布关于lab.anytao.net的应用,那么我就可以像这样来进行配置:

好了,下面在浏览器中键入lab.anytao.net,就可以打开本地部署的应用,以前的127.0.0.1或者localhost消失了,看着anytao.net,感觉是件惬意的事儿。小小的技巧可以带来很多收获,在开发阶段通过配置host,就可以直接应该你的项目域名来访问本地site了,着简直是太好了。

 

其实,这一切都很简单,雕虫小技而已,不过,让美丽的代码披上漂亮的外衣,在浏览器的地址上飞舞,不是更好吗?

 

 

更多闲言碎语,关注anytao.net

 

anytao | © 2009 Anytao.com

2009/11/27 | http://anytao.cnblogs.com/ | http://anytao.net/blog/post/2009/11/26/anytao-devstory-12-running-your-app-with-your-domain.aspx

本文以“现状”提供且没有任何担保,同时也没有授予任何权利。 | This posting is provided "AS IS" with no warranties, and confers no rights.

本文版权归作者所有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

posted @ 2009-11-27 00:15  Anytao  阅读(4173)  评论(11编辑  收藏  举报