318

传统上,我将自定义域与我的 localhost 开发服务器一起使用。类似于以下内容:

dev.example.com
dev.api.example.com

在使用 Facebook 等外部 API 时,这为我提供了很大的灵活性。这在过去使用内置的 Visual Studio 开发服务器时效果很好,因为我需要做的就是将 CNAME 添加到指向127.0.0.1.

但是,我无法让它与 IIS Express 一起使用。我尝试过的一切似乎都失败了。我什至在applicationHost.configIIS Express 的文件中添加了正确的 XML 配置,但它似乎无法像真正安装 IIS 那样识别这些条目。

<binding protocol="http" bindingInformation="*:1288:dev.example.com" />

每当我输入此行并尝试请求时,http://dev.example.com:1288我都会收到以下消息:

错误请求 - 无效的主机名

有人知道我是否遗漏了一些明显的东西吗?还是 IIS Express 团队真的缺乏预见这种用途的先见之明?

4

15 回答 15

380

This is what worked for me (Updated for VS 2013, see revision history for 2010, for VS 2015 see this: https://stackoverflow.com/a/32744234/218971):

  1. Right-click your Web Application Project ▶ PropertiesWeb, then configure the Servers section as follows:

    • Select IIS Express ▼</kbd> 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.)
  2. Optionally: Set the Start URL to http://dev.example.com

  3. 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>
    
  4. If running MVC: make sure the applicationPool is set to one of the "Integrated" options (like "Clr2IntegratedAppPool").

  5. Open your hosts file and add the line 127.0.0.1 dev.example.com.

  6. ► Start your application!

Some great advice from the comments:

  • You may need to run Visual Studio as Administrator.
  • If you want to make other devs see your IIS run netsh http add urlacl url=http://dev.example.com:80/ user=everyone
  • If you want the site to resolve for all hosts set bindingInformation="*:80:". Use any port you want, 80 is just convenient. To resolve all hosts you'll need to run Visual Studio as an administrator
于 2011-03-03T21:18:54.137 回答
164

对于Visual Studio 2015,上述答案中的步骤适用,但applicationhost.config文件位于新位置。applicationhost.config在您的“解决方案”文件夹中遵循路径,如果您升级并且在您的机器上会有两个版本,这会令人困惑。

\.vs\config

在该文件夹中,您将看到您的applicationhost.config文件

或者,您可以在解决方案文件夹中搜索 .config 文件并以这种方式找到它。

我个人使用了以下配置:

在此处输入图像描述

在我的主机文件中包含以下内容:

127.0.0.1       jam.net
127.0.0.1       www.jam.net

我的 applicationhost.config 文件中包含以下内容:

<site name="JBN.Site" id="2">
    <application path="/" applicationPool="Clr4IntegratedAppPool">
        <virtualDirectory path="/" physicalPath="C:\Dev\Jam\shoppingcart\src\Web\JBN.Site" />
    </application>
    <bindings>
        <binding protocol="http" bindingInformation="*:49707:" />
            <binding protocol="http" bindingInformation="*:49707:localhost" /> 
    </bindings>
</site>

请记住以管理员身份运行您的 Visual Studio 2015 实例!如果你不想每次我推荐这个时都这样做:

默认情况下如何以管理员身份运行 Visual Studio

我希望这对某人有所帮助,我在尝试升级到 Visual Studio 2015 时遇到了问题,并意识到我的所有配置都没有被继承。

于 2015-09-23T15:56:10.363 回答
45

将 Visual Studio 2012 与 IIS Express 一起使用时,更改现有绑定不会永久有效。(在你关闭 VS 之前它可能会起作用,但在那之后,事情就会变得一团糟。)

关键是保留现有的 localhost 绑定并在其后添加新绑定。

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

如果要允许任何主机名,完整过程如下:

  1. 创建您的 Web 应用程序,并找出它正在使用的端口(请参阅项目属性、Web 选项卡、项目 URL)。
  2. 在管理员提示符下,运行以下命令(将portnumber替换为您在 #1 中找到的端口号):

    netsh http add urlacl url="http://*:portnumber/" user=everyone
    netsh http add urlacl url="http://localhost:portnumber/" user=everyone
    

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

  1. 打开 applicationhost.config(通常在 My Documents\IIS Express\config 下),找到带有您的端口号的元素。
  2. 使用您想要的主机名再添加一个绑定(在本例中为 *)。例如:

    <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);仅具有您特定主机名的主机就足够了。

于 2013-02-19T21:19:59.293 回答
24

无效的主机名表示您在 IIS Express 配置文件中配置的实际站点(很可能)没有运行。IIS Express 没有像 IIS 那样的进程模型。


为了让您的站点运行,它需要显式启动(通过从 webmatrix 打开和访问,或者从命令行调用 iisexpress.exe(从它的安装目录)使用 /site 参数。


通常,允许将完全限定的 DNS 名称用于本地访问的步骤是让我们使用您的 DNS 名称 dev.example.com 示例

  1. 编辑 %windows%\system32\drivers\etc\hosts 文件以将 dev.example.com 映射到 127.0.0.1(需要管理员权限)。如果您控制 DNS 服务器(如 Nick 的情况),则 DNS 条目就足够了,因为不需要此步骤。
  2. 如果您通过代理访问互联网,请确保不会将 dev.example.com 转发到代理(您必须在浏览器的例外列表中输入(对于 IE,它是工具/Internet 选项/连接/局域网设置,然后转到 Proxy Server/Advanced 并将 dev.example.com 放在 exeption 列表中。
  3. 为您的站点(例如:Site1)配置 IIS Express 绑定以包含 dev.example.com。使用绑定需要管理权限。或者,可以使用 http.sys 进行一次性 URL 保留

    netsh http add urlacl url=http://dev.example.com:<port>/ user=<user_name>

  4. 在 WebMatrix 中启动iisexpress /site:Site1或打开 Site1

于 2011-01-17T21:21:11.190 回答
19

在我的 WebMatrix IIS Express 安装中,从 更改"*:80:localhost""*:80:custom.hostname"不起作用(“错误的主机名”,即使使用正确的 etc\hosts 映射),但"*:80:" 确实有效 - 并且没有其他答案所需的其他步骤。请注意,"*:80:*"不会这样做;去掉第二个星号。

于 2012-07-31T17:06:28.590 回答
10

上面的 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"

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

于 2016-08-05T16:17:33.083 回答
9

我试图将公共 IP 地址集成到我的工作流程中,但这些答案没有帮助(我喜欢将 IDE 用作 IDE)。但是上面的内容让我找到了解决方案(经过大约 2 小时的头撞墙以使其与 Visual Studio 2012 / Windows 8 集成),这就是最终为我工作的方法。

VisualStudio下生成的applicationhost.configC:\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 设置为运行,Administrator以便它可以绑定到外部地址(不是本地主机)
  • 以管理员身份运行 Visual Stuio,Administrator以便它可以以管理员身份启动进程,从而进行绑定。

最终结果是您可以浏览到192.168.2.102我的案例并进行测试(例如在 Android 模拟器中。我真的希望这对其他人有帮助,因为这对我来说绝对是一个令人讨厌的过程。

显然,这是一项我希望禁用的安全功能。

于 2013-01-22T21:54:55.130 回答
6

投票赞成的答案是有效的..这些信息对我帮助很大。我知道这个话题之前已经讨论过,但我想添加一些额外的输入。人们说您必须“手动编辑”用户 IISExpress/Config 目录中的 application.config 文件。这对我来说是个大问题,因为我想通过源代码控制将配置分发给各种开发人员。

我发现您可以使用“C:\Program Files\IIS Express\appcmd.exe”程序自动更新此文件。找出控制参数需要一段时间,但我会在这里分享我的发现。本质上,您可以创建一个运行 NETSH 命令和 APPCMD.EXE 的 .bat 文件(如果您愿意,也可以换出一个主机文件),以便使用 IIS Express 轻松配置主机头文件。

您的安装 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']

我希望这些信息对某人有所帮助。我花了一些时间才发现。

于 2015-01-01T11:15:20.890 回答
4

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

  1. 使用 WebMatrix 从您的 Web 应用程序根目录中的文件夹创建一个新站点。
  2. 关闭 WebMatrix。
  3. 打开%USERPROFILE%\My Documents\IISExpress\config\applicationhost.config (Win​​dows XP。Vista 和 7 路径将类似)并编辑配置块中的站点定义,<sites>使其符合以下行:

    <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 设置为“集成”选项之一。

于 2011-02-18T18:37:13.227 回答
4

此方法已通过 ASP.NET Core 3.1 和 Visual Studio 2019 进行测试和使用。

.vs\PROJECTNAME\config\applicationhost.config

将“*:44320:localhost”更改为“*:44320:*”。

<bindings>
    <binding protocol="http" bindingInformation="*:5737:localhost" />
    <binding protocol="https" bindingInformation="*:44320:*" />
</bindings>

两个链接都有效:

现在,如果您希望应用程序使用自定义域,只需将以下行添加到主机文件中:

C:\Windows\System32\drivers\etc\hosts

127.0.0.1 customdomain

现在:

  • https://customdomain:44320

注意:如果您的应用程序在没有 SSL 的情况下运行,请更改协议 =“http”部分。

于 2020-08-16T12:50:28.320 回答
3

大卫的解决方案很好。但是我发现<script>alert(document.domain);</script>页面中的仍然会提示“localhost”,因为项目 URL 仍然是 localhost,即使它已被覆盖http://dev.example.com。我遇到的另一个问题是,即使我按照 David Murdoch 的建议使用 80 端口号禁用了 Skype,它也会提醒我端口 80 已被使用。所以我想出了另一个更容易的解决方案:

  1. 以管理员身份运行记事本,打开C:\Windows\System32\drivers\etc\hosts,添加127.0.0.1 mydomain,保存文件;
  2. 使用 Visual Studio 2013 打开 Web 项目(注意:也必须以管理员身份运行),右键单击项目 -> 属性 -> Web,(假设“IIS Express”选项下的 Project Url 为http://localhost:33333/),然后将其更改为http://localhost:33333/注意http://mydomain:333333/ :更改后,您既不能单击 Project Url 框右侧的“创建虚拟目录”按钮,也不能单击 Visual Studio 的“保存”按钮,否则将无法成功。您可以在下一步 3 之后保存您的设置。
  3. 打开 %USERPROFILE%\My Documents\IISExpress\config\applicationhost.config,搜索“33333:localhost”,然后将其更新为“33333:mydomain”并保存文件。
  4. 按照步骤 2 中的说明保存您的设置。
  5. 右键单击 Visual Studio 中的网页,然后单击“在浏览器中查看”。现在页面将在 下打开http://mydomain:333333/,并且<script>alert(document.domain);</script>在页面中会提示“mydomain”。

注意:上面列出的端口号假定为 33333。您需要将其更改为您的 Visual Studio 设置的端口号。

帖子编辑:今天我尝试使用另一个域名并收到以下错误:无法启动 IIS Express Web 服务器。注册 URL 失败...访问被拒绝。(0x80070005)。我通过右键单击 Windows 任务栏右上角的 IIS Express 图标退出 IIS Express,然后以管理员身份重新启动我的 Visual Studio,问题就消失了。

于 2016-08-31T19:44:24.957 回答
2

我尝试了以上所有方法,没有任何效果。解决问题的方法是在 hosts 文件中添加 IPv6 绑定。在@David Murdochs 回答的第 5 步中,添加两行而不是一行,即:

127.0.0.1 dev.example.com
::1 dev.example.com

$ ping localhost我通过从命令行检查来解决这个问题,它曾经返回:

Reply from 127.0.0.1: bytes=32 time<1ms TTL=128

相反,它现在返回:

Reply from ::1: time<1ms

我不知道为什么,但出于某种原因,IIS Express 开始使用 IPv6 而不是 IPv4。

于 2019-11-22T13:53:14.487 回答
1

以防万一有人需要...

我的要求是:

  • 启用 SSL
  • 自定义域
  • 在(默认)端口中运行:443

在 IISExpress 中设置此 URL:http://my.customdomain.com

为了进行设置,我使用了以下设置:

项目网址:http://localhost:57400

开始网址:http://my.customdomain.com

/.vs/{solution-name}/config/applicationhost.config设置:

<site ...>
    <application>
        ...
    </application>
    <bindings>
        <binding protocol="http" bindingInformation="*:57400:" />
        <binding protocol="https" bindingInformation="*:443:my.customdomain.com" />
    </bindings>
</site>
于 2021-02-16T19:54:43.667 回答
0

我为此使用了 iisexpress-proxy(来自 npm)。

https://github.com/icflorescu/iisexpress-proxy

于 2016-02-23T14:50:39.820 回答
0

把这个留在这里以防万一有人需要......

我需要在 IIS Express 中为 Wordpress 多站点设置设置自定义域,但在我以管理员身份运行 Webmatrix/Visual Studio 之前没有任何效果。然后我能够将子域绑定到同一个应用程序。

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

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

于 2017-06-13T22:27:34.570 回答