传统上,我在本地主机开发服务器上使用自定义域。大致如下:
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团队真的缺乏预见到这种类型的使用?
我试图将公共IP地址集成到我的工作流程中,这些答案没有帮助(我喜欢使用IDE作为IDE)。但以上这些让我找到了解决方案
(我花了大约2个小时的时间才把它与Visual Studio 2012 / Windows 8集成起来)下面是我最终得到的结果。
applicationhost。C:\Users\usr\Documents\IISExpress\config
<site name="MySite" id="1">
<application path="/" applicationPool="Clr4IntegratedAppPool">
<virtualDirectory path="/" physicalPath="C:\Users\usr\Documents\Visual Studio 2012\Projects\MySite" />
</application>
<bindings>
<binding protocol="http" bindingInformation="*:8081:localhost" />
<binding protocol="http" bindingInformation="*:8082:localhost" />
<binding protocol="http" bindingInformation="*:8083:192.168.2.102" />
</bindings>
</site>
设置IISExpress以管理员身份运行,以便它可以绑定到外部地址(而不是本地主机)
以管理员身份运行Visual studio,这样它就可以以管理员身份启动流程,从而允许发生绑定。
最终结果是,在我的例子中,您可以浏览到192.168.2.102并进行测试(例如在Android模拟器中)。我真的希望这能帮助到其他人,因为这对我来说绝对是一个恼人的过程。
显然,这是一个安全功能,我希望看到禁用。
赞成的答案是有效的。这些信息对我帮助很大。我知道这个话题之前已经讨论过了,但我想添加一些额外的输入。
人们说您必须“手动编辑”应用程序。“Users IISExpress/ config”目录下的config文件。这对我来说是个大问题,因为我想通过源代码控制将配置分发给不同的开发人员。
我发现你可以使用“C:\Program Files\IIS Express\appcmd.exe”程序自动更新这个文件。找到控制参数需要一段时间,但我将在这里分享我的发现。本质上,您可以创建一个.bat文件,运行NETSH命令和APPCMD.EXE(如果您愿意,也可以换出一个主机文件),以便使用IIS Express轻松配置主机头。
你的install bat文件看起来是这样的:
netsh http add urlacl url=http://yourcustomdomain.com:80/ user=everyone
"C:\Program Files\IIS Express\appcmd.exe" set site "MyApp.Web" /+bindings.[protocol='http',bindingInformation='*:80:yourcustomdomain.com']
我也会做一个“卸载”bat文件,将清理这些绑定..(因为很多时候我只是伪造DNS,这样我就可以在主机名敏感的代码上工作)
netsh http delete urlacl url=http://yourcustomdomain.com:80/
"C:\Program Files\IIS Express\appcmd.exe" set site "MyApp.Web" /-bindings.[protocol='http',bindingInformation='*:80:yourcustomdomain.com']
我希望这些信息对某人有所帮助。我花了一点时间才发现。
大卫的解决办法很好。但是我发现页面中的<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,问题就解决了。
无效的主机名表明您在IIS Express配置文件中配置的实际站点(很可能)未运行。IIS Express没有IIS那样的流程模型。
要运行站点,需要显式启动(通过从webmatrix打开并访问,或者通过命令行调用iisexpress.exe(从它的安装目录),并使用/site参数。
通常,允许使用完全限定的DNS名称进行本地访问的步骤如下
让我们使用DNS名称dev.example.com的示例
edit %windows%\system32\drivers\etc\hosts file to map dev.example.com to 127.0.0.1 (admin privilege required). If you control DNS server (like in Nick's case) then the DNS entry is sufficient as this step is not needed.
If you access internet through proxy, make sure the dev.example.com will not be forwared to proxy (you have to put in on the exception list in your browser (for IE it would be Tools/Internet Options/Connections/Lan Settings, then go to Proxy Server/Advanced and put dev.example.com on the exeption list.
Configure IIS Express binding for your site (eg:Site1) to include dev.example.com. Administrative privilege will be needed to use the binding. Alternatively, a one-time URL reservation can be made with http.sys using
netsh http add urlacl url=http://dev.example.com:<port>/ user=<user_name>
start iisexpress /site:Site1 or open Site1 in WebMatrix