传统上,我在本地主机开发服务器上使用自定义域。大致如下:
dev.example.com
dev.api.example.com
这为我在使用外部api(如Facebook)时提供了很大的灵活性。这在过去与内置的Visual Studio Development Server一起工作得很好,因为我所需要做的只是向指向127.0.0.1的DNS记录添加一个CNAME。
但是,我还不能让它与IIS Express一起工作。我所做的一切努力似乎都失败了。我甚至向applicationHost添加了正确的XML配置。IIS Express的配置文件,但它似乎不能像真正安装IIS那样识别有效的条目。
<binding protocol="http" bindingInformation="*:1288:dev.example.com" />
每当我输入这一行并尝试请求http://dev.example.com:1288时,我都会收到以下消息:
错误请求-无效的主机名
有人知道我是否遗漏了什么明显的东西吗?或者IIS Express团队真的缺乏预见到这种类型的使用?
大卫的解决办法很好。但是我发现页面中的<script>警报(document.domain);</script>仍然警告“localhost”,因为项目Url仍然是localhost,即使它已经被http://dev.example.com覆盖。我遇到的另一个问题是,它提醒我端口80已经在使用,即使我已经禁用Skype使用80端口号,由大卫默多克推荐。所以我想出了另一个更简单的解决方案:
Run Notepad as administrator, and open the C:\Windows\System32\drivers\etc\hosts, add 127.0.0.1 mydomain, and save the file;
Open the web project with Visual Studio 2013 (Note: must also run as administrator), right-click the project -> Properties -> Web, (lets suppose the Project Url under the "IIS Express" option is http://localhost:33333/), then change it from http://localhost:33333/ to http://mydomain:333333/
Note: After this change, you should neither click the "Create Virtual Directory" button on the right of the Project Url box nor click the Save button of the Visual Studio as they won't be succeeded. You can save your settings after next step 3.
Open %USERPROFILE%\My Documents\IISExpress\config\applicationhost.config, search for "33333:localhost", then update it to "33333:mydomain" and save the file.
Save your setting as mentioned in step 2.
Right click a web page in your visual studio, and click "View in Browser". Now the page will be opened under http://mydomain:333333/, and <script>alert(document.domain);</script> in the page will alert "mydomain".
注意:上面列出的端口号假设为33333。您需要将其更改为您的visual studio设置的端口号。
Post编辑:今天我尝试了另一个域名,得到以下错误:无法启动IIS Express Web服务器。注册URL失败…访问被拒绝。(0 x80070005)。我通过右键单击Windows任务栏右下角的IIS Express图标退出IIS Express,然后以管理员身份重新启动我的visual studio,问题就解决了。
大卫的解决办法很好。但是我发现页面中的<script>警报(document.domain);</script>仍然警告“localhost”,因为项目Url仍然是localhost,即使它已经被http://dev.example.com覆盖。我遇到的另一个问题是,它提醒我端口80已经在使用,即使我已经禁用Skype使用80端口号,由大卫默多克推荐。所以我想出了另一个更简单的解决方案:
Run Notepad as administrator, and open the C:\Windows\System32\drivers\etc\hosts, add 127.0.0.1 mydomain, and save the file;
Open the web project with Visual Studio 2013 (Note: must also run as administrator), right-click the project -> Properties -> Web, (lets suppose the Project Url under the "IIS Express" option is http://localhost:33333/), then change it from http://localhost:33333/ to http://mydomain:333333/
Note: After this change, you should neither click the "Create Virtual Directory" button on the right of the Project Url box nor click the Save button of the Visual Studio as they won't be succeeded. You can save your settings after next step 3.
Open %USERPROFILE%\My Documents\IISExpress\config\applicationhost.config, search for "33333:localhost", then update it to "33333:mydomain" and save the file.
Save your setting as mentioned in step 2.
Right click a web page in your visual studio, and click "View in Browser". Now the page will be opened under http://mydomain:333333/, and <script>alert(document.domain);</script> in the page will alert "mydomain".
注意:上面列出的端口号假设为33333。您需要将其更改为您的visual studio设置的端口号。
Post编辑:今天我尝试了另一个域名,得到以下错误:无法启动IIS Express Web服务器。注册URL失败…访问被拒绝。(0 x80070005)。我通过右键单击Windows任务栏右下角的IIS Express图标退出IIS Express,然后以管理员身份重新启动我的visual studio,问题就解决了。
我试了以上所有的方法,都没用。解决这个问题的方法是在hosts文件中添加IPv6绑定。在@David murdoch回答的第五步中,添加两行而不是一行,即:
127.0.0.1 dev.example.com
::1 dev.example.com
我通过从命令行检查$ ping localhost来计算出来,它通常返回:
来自127.0.0.1的回复:bytes=32 time<1ms TTL=128
相反,它现在返回:
回复from::1:时间<1ms
我不知道为什么,但是由于某种原因,IIS Express开始使用IPv6而不是IPv4。
这是为我工作(更新的VS 2013,见2010年的修订历史,VS 2015见这个:https://stackoverflow.com/a/32744234/218971):
Right-click your Web Application Project ▶ Properties ▶ Web, then configure the Servers section as follows:
Select IIS Express ▼ from the drop down
Project Url: http://localhost
Override application root URL: http://dev.example.com
Click Create Virtual Directory (if you get an error here you may need to disable IIS 5/6/7/8, change IIS's Default Site to anything but port :80, make sure Skype isn't using port 80, etc.)
Optionally: Set the Start URL to http://dev.example.com
Open %USERPROFILE%\My Documents\IISExpress\config\applicationhost.config (Windows XP, Vista, and 7) and edit the site definition in the <sites> config block to be along the lines of the following:
<site name="DevExample" id="997005936">
<application path="/" applicationPool="Clr2IntegratedAppPool">
<virtualDirectory
path="/"
physicalPath="C:\path\to\application\root" />
</application>
<bindings>
<binding
protocol="http"
bindingInformation=":80:dev.example.com" />
</bindings>
<applicationDefaults applicationPool="Clr2IntegratedAppPool" />
</site>
If running MVC: make sure the applicationPool is set to one of the "Integrated" options (like "Clr2IntegratedAppPool").
Open your hosts file and add the line 127.0.0.1 dev.example.com.
► Start your application!
评论中有一些很棒的建议:
您可能需要以管理员身份运行Visual Studio。
如果你想让其他开发人员看到你的IIS,运行netsh http add urlacl url=http://dev.example.com:80/ user=everyone
如果您希望站点为所有主机解析,请设置bindingInformation="*:80:"。
使用任何端口,80只是方便。要解析所有主机,您需要以管理员身份运行Visual Studio