传统上,我在本地主机开发服务器上使用自定义域。大致如下:

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团队真的缺乏预见到这种类型的使用?


当前回答

在使用Visual Studio 2012和IIS Express时,更改现有绑定不会永久工作。(它可能在你关闭VS之前有效,但在那之后,事情就变得非常混乱了。)

关键是保持现有的本地主机绑定并在其之后添加一个新绑定。

除非您以管理员身份运行,否则还需要运行netsh add urlacl(以标准用户身份授予自己运行非本地主机站点的权限)。

如果要允许任意主机名,整个过程如下:

创建您的web应用程序,并找出它正在使用的端口(参见项目属性,web选项卡,项目Url)。 在管理员提示符下,运行以下命令(将portnumber替换为您在#1中计算出的端口号): Netsh HTTP添加urlacl url="http://*:portnumber/" user=everyone Netsh HTTP添加urlacl url="http://localhost:portnumber/" user=everyone

您也可以使用您的用户名(DOMAIN\ user),而不是所有人,以获得更好的安全性。

Open applicationhost.config (usually under My Documents\IIS Express\config), and find the element with your port number. Add one more binding with the host name you want (in this case, *). For example: <site name="MvcApplication1" id="2"> <application path="/" applicationPool="Clr4IntegratedAppPool"> <virtualDirectory path="/" physicalPath="C:\sites\MvcApplication1" /> </application> <bindings> <binding protocol="http" bindingInformation="*:12853:localhost" /> <binding protocol="http" bindingInformation="*:12853:*" /> </bindings> </site>

注意,如果想打开所有主机名(*),需要两个netsh命令(一个用于*,一个用于localhost)。如果您只想打开一个特定的主机名,则不需要严格地使用第二个netsh命令(localhost);只有具有特定主机名的一个就足够了。

其他回答

把这个留在这里,以防有人需要…

我需要在IIS Express中为Wordpress Multisite设置自定义域,但在我以管理员身份运行Webmatrix/Visual Studio之前,任何东西都不起作用。然后我就可以将子域绑定到同一个应用程序。

<bindings> 
    <binding protocol="http" bindingInformation="*:12345:localhost" />
    <binding protocol="http" bindingInformation="*:12345:whatever.localhost" />
</bindings>

然后转到http://whatever.localhost:12345/将运行。

我试图将公共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模拟器中)。我真的希望这能帮助到其他人,因为这对我来说绝对是一个恼人的过程。

显然,这是一个安全功能,我希望看到禁用。

按照Jaro的建议,我可以在Windows XP和IIS Express(通过Web Matrix安装)下进行小的修改,并且不局限于localhost。这只是一个正确设置绑定的问题。

Use WebMatrix to create a new site from folder in your web application root. Close WebMatrix. Open %USERPROFILE%\My Documents\IISExpress\config\applicationhost.config (Windows XP. Vista and 7 paths will be similar) 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>

如果运行MVC,则将applicationPool设置为“Integrated”选项之一。

像上面的Jessa Flint一样,我不想手动编辑.vs\config\applicationhost。config,因为我希望更改能够持久地保存在源代码控制中。我也不想有一个单独的批处理文件。我使用的是VS 2015。

项目属性→构建事件→预构建事件命令行:


::The following configures IIS Express to bind to any address at the specified port

::remove binding if it already exists
"%programfiles%\IIS Express\appcmd.exe" set site "MySolution.Web" /-bindings.[protocol='http',bindingInformation='*:1167:'] /apphostconfig:"$(SolutionDir).vs\config\applicationhost.config"

::add the binding
"%programfiles%\IIS Express\appcmd.exe" set site "MySolution.Web" /+bindings.[protocol='http',bindingInformation='*:1167:'] /apphostconfig:"$(SolutionDir).vs\config\applicationhost.config"

只需确保将端口号更改为所需的端口。

无效的主机名表明您在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