如何在Visual Studio项目的applicationhost.config中添加本地域名映射
在Visual Studio的applicationhost.config中配置本地域名映射的位置与操作方法
1. 找到applicationhost.config文件
这个文件一般在你项目的.vs\config目录下(.vs是隐藏文件夹,得先显示隐藏文件才能看到);要是找不到项目级的,全局配置路径是%userprofile%\Documents\IISExpress\config\applicationhost.config,但项目级的配置优先级更高。
2. 定位到站点绑定节点
打开文件后,找到和你项目名称对应的<site>节点(看name属性就能识别),然后找到该节点下的<bindings>标签,后续的配置都要加在这里面。
3. 添加域名绑定配置
给每个要映射的域名加一条<binding>元素,格式参考下面的代码:
<binding protocol="http" bindingInformation="*:80:localhost" /> <binding protocol="http" bindingInformation="*:80:en.localhost" /> <binding protocol="http" bindingInformation="*:80:api.localhost" /> <binding protocol="http" bindingInformation="*:80:en.api.localhost" />
如果你的项目用HTTPS,就把protocol改成https,端口换成项目对应的HTTPS端口(比如443或者自定义的44300这类),示例:
<binding protocol="https" bindingInformation="*:44300:localhost" />
4. 补做系统hosts文件配置(必须步骤)
光在applicationhost.config里配绑定还没用,得让系统能把这些域名解析到127.0.0.1,所以要修改C:\Windows\System32\drivers\etc\hosts文件,加上你给出的内容:
127.0.0.1 localhost 127.0.0.1 en.localhost 127.0.0.1 api.localhost 127.0.0.1 en.api.localhost
修改hosts需要管理员权限,保存完记得运行ipconfig /flushdns命令刷新DNS缓存。
5. 重启服务和IDE
配置完后,关掉当前运行的IIS Express,再重启Visual Studio,重新启动项目,就能用这些域名访问你的站点了。
内容的提问来源于stack exchange,提问作者mz1378
相关产品推荐
相关产品推荐

