将自定义域与 IIS Express 结合使用

发布于 2024-10-12 04:56:47 字数 666 浏览 3 评论 0原文

传统上,我将自定义域与本地主机开发服务器一起使用。大概是这样的:

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

这为我在使用 Facebook 等外部 API 时提供了很大的灵活性。这在过去与内置的 Visual Studio 开发服务器配合使用非常有效,因为我所需要做的就是向那些指向 127.0.0.1 的 DNS 记录添加 CNAME。

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

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

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

错误请求 - 无效主机名

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

Traditionally I use custom domains with my localhost development server. Something along the lines of:

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

This has provided me a ton of flexibility when working with external APIs such as Facebook. This has worked great in the past with the built-in Visual Studio Development Server because all I needed to do was add a CNAME to those DNS records pointing to 127.0.0.1.

However, I have not been able to get this to work with IIS Express. Everything I have tried seems to have failed. I have even added the correct XML config to the applicationHost.config file for IIS Express, but it doesn't seem to recognize the entries as valid as a true install of IIS would.

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

Whenever I enter this line and try to request http://dev.example.com:1288 I get the following message:

Bad Request - Invalid Hostname

Does anybody know if I am missing something obvious? Or did the IIS Express team really lack the foresight to see this type of use?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(16

我做我的改变 2024-10-19 04:56:47

这对我有用(针对 VS 2013 进行了更新,请参阅 2010 年的修订历史记录,对于 VS 2015 请参阅:https://stackoverflow.com/a/32744234/218971):

  1. 右键单击您的 Web 应用程序项目 ▶ PropertiesWeb,然后配置 Servers 部分如下:

    • 从下拉菜单中选择IIS Express ▼
    • 项目网址:http://localhost
    • 覆盖应用程序根 URL:http://dev.example.com
    • 点击创建虚拟目录(如果此处出现错误,您可能需要禁用 IIS 5/6/7/8,将 IIS 的默认站点更改为除端口之外的任何内容) :80,请确保 Skype 未使用端口 80 等)
  2. 可选:将起始 URL 设置为 http://dev.example.com

  3. 打开 %USERPROFILE%\My Documents\IISExpress\config\applicationhost。 config(Windows XP、Vista 和 7)并在 配置块中编辑站点定义,使其符合以下内容:

    <站点名称=“DevExample”id=“997005936”>
        <应用程序路径=“/”applicationPool=“Clr2IntegratedAppPool”>
            <虚拟目录
                路径=“/”
                physicalPath="C:\path\to\application\root" />
        
        <绑定>
            <绑定
                协议=“http”
                bindingInformation=":80:dev.example.com" />
        
        >
    
    
  4. 如果运行 MVC:请确保将 applicationPool 设置为“Integrated”选项之一(例如“Clr2IntegratedAppPool”)。

  5. 打开您的hosts 文件并添加以下行127.0.0.1 dev.example.com

  6. ► 开始您的应用程序!

评论中的一些很好的建议:

  • 您可能需要以管理员身份运行 Visual Studio。
  • 如果您想让其他开发人员看到您的 IIS 运行 netsh http add urlacl url=http://dev.example.com:80/ user=everyone
  • 如果您希望站点针对所有主机进行解析,请设置 bindingInformation="*:80:"
    使用任何你想要的端口,80 就很方便。要解析所有主机,您需要以管理员身份运行 Visual Studio

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 ▼ 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
酒几许 2024-10-19 04:56:47

对于 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 实例!< /strong> 如果您不想每次我推荐时都这样做:

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

我在尝试升级到 Visual Studio 2015 时遇到了问题,并意识到我的所有配置都没有被保留。

For Visual Studio 2015 the steps in the above answers apply but the applicationhost.config file is in a new location. In your "solution" folder follow the path, this is confusing if you upgraded and would have TWO versions of applicationhost.config on your machine.

\.vs\config

Within that folder you will see your applicationhost.config file

Alternatively you could just search your solution folder for the .config file and find it that way.

I personally used the following configuration:

enter image description here

With the following in my hosts file:

127.0.0.1       jam.net
127.0.0.1       www.jam.net

And the following in my applicationhost.config file:

<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>

Remember to run your instance of visual studio 2015 as an administrator! If you don't want to do this every time I recomend this:

How do I run Visual Studio as an administrator by default?

I had issues when trying to upgrade to visual studio 2015 and realized that none of my configurations were being carried over.

A君 2024-10-19 04:56:47

将 Visual Studio 2012 及更高版本与 IIS Express 结合使用时,更改现有绑定不会永久有效。 (在您关闭 VS 之前它可能会起作用,但在那之后,事情就会变得非常混乱。)

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

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

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

  1. 创建您的 Web 应用程序,并找出它正在使用的端口(请参阅项目属性、Web 选项卡、项目 Url)。

  2. 在管理员提示符下,运行以下命令(将端口号替换为您在 #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(通常位于“我的文档\IIS Express\config”下),然后找到包含您的端口号的元素。

  2. 再添加一个与所需主机名的绑定(在本例中为 *)。例如:

     <站点名称=“MvcApplication1”id=“2”>
         <应用程序路径=“/”applicationPool=“Clr4IntegratedAppPool”>
             
         
         <绑定>
             <绑定协议=“http”绑定信息=“*:12853:localhost”/>
             <绑定协议=“http”绑定信息=“*:12853:*”/>
         
     
    

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

When using Visual Studio 2012 and above with IIS Express, changing an existing binding does not work permanently. (It may work until you close VS, but after that, things get really messed up.)

The key is keeping the existing localhost binding and adding a new binding after it.

Unless you're running as administrator, you'll also need to run netsh http add urlacl (to give yourself permissions to run a non-localhost site as a standard user).

If you want to allow any host name, the full process is as follows:

  1. Create your web application, and find out what port it is using (see project properties, Web tab, Project Url).

  2. From an administrator prompt, run the following commands (replacing portnumber with the port number you figured out in #1):

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

You can also use your user name (DOMAIN\USER) instead of everyone for better security.

  1. Open applicationhost.config (usually under My Documents\IIS Express\config), and find the element with your port number.

  2. 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>
    

Note that, if want to open up all host names (*), you'll need two netsh commands (one for * and one for localhost). If you only want to open up a specific host name, you don't strictly need the second netsh command (localhost); just the one with your specific host name is sufficient.

夏雨凉 2024-10-19 04:56:47

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


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


一般来说,允许完全限定的步骤用于本地访问的 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 选项/连接/Lan 设置,然后转到代理服务器/高级并将 dev.example.com 放入例外列表中
  3. 为您的站点(例如:Site1)配置 IIS Express 绑定以包含 dev.example.com。或者,可以使用

    通过 http.sys 进行一次性 URL 预留。

    netsh http add urlacl url=http://dev.example.com:/ user=

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

The invalid hostname indicates that the actual site you configured in the IIS Express configuration file is (most likely) not running. IIS Express doesn't have a process model like IIS does.


For your site to run it would need to be started explicitly (either by opening and accessing from webmatrix, or from command line calling iisexpress.exe (from it's installation directory) with the /site parameter.


In general, the steps to allow fully qualified DNS names to be used for local access are
Let's use your example of the DNS name dev.example.com

  1. 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.
  2. 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.
  3. 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>

  4. start iisexpress /site:Site1 or open Site1 in WebMatrix

倒带 2024-10-19 04:56:47

在我的 WebMatrix IIS Express 安装上,从 "*:80:localhost" 更改为 "*:80:custom.hostname" 不起作用(“Bad Hostname”,甚至具有正确的 etc\hosts 映射),但是 "*:80:" 确实工作 - 并且没有此处其他答案所需的任何额外步骤。请注意, "*:80:*" 不会这样做;省略第二个星号。

On my WebMatrix IIS Express install changing from "*:80:localhost" to "*:80:custom.hostname" didn't work ("Bad Hostname", even with proper etc\hosts mappings), but "*:80:" did work--and with none of the additional steps required by the other answers here. Note that "*:80:*" won't do it; leave off the second asterisk.

深海不蓝 2024-10-19 04:56:47

就像上面的 Jessa Flint 一样,我不想手动编辑 .vs\config\applicationhost.config 因为我希望更改保留在源代码管理中。我也不想有一个单独的批处理文件。我正在使用 VS 2015

项目属性→构建事件→预构建事件命令行:
项目屏幕截图Properties


::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"

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

Like Jessa Flint above, I didn't want to manually edit .vs\config\applicationhost.config because I wanted the changes to persist in source control. I also didn't want to have a separate batch file. I'm using VS 2015.

Project Properties→Build Events→Pre-build event command line:
Screenshot of project properties


::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"

Just make sure you change the port number to your desired port.

千纸鹤 2024-10-19 04:56:47

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

applicationhost.config 由 VisualStudio 在 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 Stuio,以便它可以以管理员身份启动进程,从而允许进行绑定。

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

显然它是一项我希望看到禁用的安全功能。

I was trying to integrate the public IP Address into my workflow and these answers didn't help (I like to use the IDE as the IDE). But the above lead me to the solution
(after about 2 hours of beating my head against a wall to get this to integrate with Visual Studio 2012 / Windows 8) here's what ended up working for me.

applicationhost.config generated by VisualStudio under 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>
  • Set IISExpress to run as Administrator so that it can bind to outside addresses (not local host)
  • Run Visual Stuio as an Administrator so that it can start the process as an administrator allowing the binding to take place.

The net result is you can browse to 192.168.2.102 in my case and test (for instance in an Android emulator. I really hope this helps someone else as this was definitely an irritating process for me.

Apparently it is a security feature which I'd love to see disabled.

得不到的就毁灭 2024-10-19 04:56:47

投票的答案是有效的..这些信息对我帮助很大。我知道这个主题之前已经讨论过,但我想添加一些额外的意见。
人们说您必须“手动编辑”Users 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']

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

The up-voted answer is valid.. and this information helped me quite a bit. I know this topic has been discussed before but I wanted to add some additional input.
People are saying that you must "manually edit" the application.config file in the Users IISExpress/Config directory. This was a big issue for me because I wanted to distribute the configuration via Source control to various developers.

What I found is that you can automate the updating of this file using the "C:\Program Files\IIS Express\appcmd.exe" program. It took a while to find out the control parameters but Ill share my findings here. Essentially you can make a .bat file that runs both the NETSH command and the APPCMD.EXE (and perhaps swap out a host file if you like) to make host header configuration easy with IIS Express.

Your install bat file would look something like this:

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']

I also will make a "Uninstall" bat file that will clean up these bindings..(because often times Im just faking out DNS so that I can work on code that is host name sensitive)

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']

I hope this information is helpful to someone.. It took me a bit to uncover.

冷心人i 2024-10-19 04:56:47

此方法已经过测试并适用于 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>

两个链接都有效:

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

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

127.0.0.1 customdomain

现在:

  • https://customdomain:44320

注意:如果您的应用程序在没有 SSL 的情况下工作,请更改 protocol="http" 部分。

This method has been tested and worked with ASP.NET Core 3.1 and Visual Studio 2019.

.vs\PROJECTNAME\config\applicationhost.config

Change "*:44320:localhost" to "*:44320:*".

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

Both links work:

Now if you want the app to work with the custom domain, just add the following line to the host file:

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

127.0.0.1 customdomain

Now:

  • https://customdomain:44320

NOTE: If your app works without SSL, change the protocol="http" part.

小巷里的女流氓 2024-10-19 04:56:47

按照 Jaro 的建议,我只需稍作修改就可以在 Windows XP 和 IIS Express(通过 Web Matrix 安装)下运行该程序,并且不仅限于本地主机。只需正确设置绑定即可。

  1. 使用 WebMatrix 从 Web 应用程序根目录中的文件夹创建新站点。
  2. 关闭 WebMatrix。
  3. 打开 %USERPROFILE%\My Documents\IISExpress\config\applicationhost.config (Windows XP、Vista 和 7 路径类似) 并在 中编辑站点定义> 配置块应遵循以下内容:
    <站点名称=“DevExample”id=“997005936”>
        <应用程序路径=“/”applicationPool=“Clr2IntegratedAppPool”>
            <虚拟目录
                路径=“/”
                physicalPath="C:\path\to\application\root" />
        
        <绑定>
            <绑定
                协议=“http”
                bindingInformation="*:80:dev.example.com" />
        
        >
    

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

Following Jaro's advice, I was able to get this working under Windows XP and IIS Express (installed via Web Matrix) with a small modification and was not limited to only localhost. It's just a matter of setting the bindings correctly.

  1. Use WebMatrix to create a new site from folder in your web application root.
  2. Close WebMatrix.
  3. 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>

If running MVC, then keep the applicationPool set to one of the "Integrated" options.

月棠 2024-10-19 04:56:47

大卫的解决方案很好。但我发现页面中的 仍然警告“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”选项下的项目 URL 为 http://localhost:33333/),然后将其从 http://localhost:33333/ 更改代码> 到 http://mydomain:333333/
    注意:更改后,您既不能单击“项目 URL”框右侧的“创建虚拟目录”按钮,也不能单击 Visual Studio 的“保存”按钮,否则不会成功。您可以在下一步 3 后保存设置。
  3. 打开 %USERPROFILE%\My Documents\IISExpress\config\applicationhost.config,搜索“33333:localhost”,然后将其更新为“33333:mydomain”并保存文件。
  4. 按照步骤 2 中所述保存设置。
  5. 右键单击视觉工作室中的网页,然后单击“在浏览器中查看”。现在页面将在 http://mydomain:333333/ 下打开,页面中的 将提醒“mydomain”。

注意:上面列出的端口号假设为33333。您需要将其更改为您的视觉工作室设置的端口号。

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

David's solution is good. But I found the <script>alert(document.domain);</script> in the page still alerts "localhost" because the Project Url is still localhost even if it has been override with http://dev.example.com. Another issue I run into is that it alerts me the port 80 has already been in use even if I have disabled the Skype using the 80 port number as recommended by David Murdoch. So I have figured out another solution that is much easier:

  1. Run Notepad as administrator, and open the C:\Windows\System32\drivers\etc\hosts, add 127.0.0.1 mydomain, and save the file;
  2. 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.
  3. Open %USERPROFILE%\My Documents\IISExpress\config\applicationhost.config, search for "33333:localhost", then update it to "33333:mydomain" and save the file.
  4. Save your setting as mentioned in step 2.
  5. 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".

Note: The port number listed above is assumed to be 33333. You need to change it to the port number set by your visual studio.

Post edited: Today I tried with another domain name and got the following error: Unable to launch the IIS Express Web server. Failed to register URL... Access is denied. (0x80070005). I exit the IIS Express by right clicking the IIS Express icon at the right corner in the Windows task bar, and then re-start my visual studio as administrator, and the issue is gone.

并安 2024-10-19 04:56:47

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

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

我通过从命令行检查 $ ping localhost 来计算出来,该命令用于返回:

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

相反,它现在返回:

Reply from ::1: time<1ms

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

I tried all of above, nothing worked. What resolved the issue was adding IPv6 bindings in the hosts file. In step 5 of @David Murdochs answer, add two lines instead of one, i.e.:

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

I figured it out by checking $ ping localhost from command line, which used to return:

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

Instead, it now returns:

Reply from ::1: time<1ms

I don't know why, but for some reason IIS Express started using IPv6 instead of IPv4.

飞烟轻若梦 2024-10-19 04:56:47

以防万一有人可能需要...

我的要求是:

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

在 IISExpress 中设置此 URL:http://my.customdomain。 为了进行设置

,我使用了以下设置:

项目 URL:http://localhost:57400

启动 URL: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>

Just in case if someone may need...

My requirement was:

  • SSL enabled
  • Custom domain
  • Running in (default) port: 443

Setup this URL in IISExpress: http://my.customdomain.com

To setup this I used following settings:

Project Url: http://localhost:57400

Start URL: http://my.customdomain.com

/.vs/{solution-name}/config/applicationhost.config settings:

<site ...>
    <application>
        ...
    </application>
    <bindings>
        <binding protocol="http" bindingInformation="*:57400:" />
        <binding protocol="https" bindingInformation="*:443:my.customdomain.com" />
    </bindings>
</site>
浅沫记忆 2024-10-19 04:56:47

我的目标是用 URL 中的 127.0.0.1 替换 localhost

最初我收到 Bad Request - Invalid Hostname

这对我有用(Microsoft Visual Studio Professional 2022(64 位)- 当前 - 版本 17.6.4)

以管理员身份运行 cmd并运行这些命令(来自 dmatson 的答案):

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

备份 \.vs\config\applicationhost.config

编辑 \.vs\config\applicationhost.config 以将其添加到应用的绑定部分中(将 44300 替换为您自己的端口号):

<binding protocol="https" bindingInformation="*:44300:localhost" />
<binding protocol="https" bindingInformation="*:44300:*" />

确保它按该顺序列出。

如果 VS 已打开,请将其关闭。

以管理员身份运行 VS,您应该能够访问 https://127.0.0.1:portnumber

My goal was to replace localhost with 127.0.0.1 in the URL.

Originally I was getting Bad Request - Invalid Hostname

This worked for me (Microsoft Visual Studio Professional 2022 (64-bit) - Current - Version 17.6.4)

Run cmd as admin and run these commands (from dmatson's answer):

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

Make a backup of \.vs\config\applicationhost.config.

Edit \.vs\config\applicationhost.config to have this in your app's bindings section (replace 44300 with your own port number):

<binding protocol="https" bindingInformation="*:44300:localhost" />
<binding protocol="https" bindingInformation="*:44300:*" />

Make sure it's listed in that order.

Close VS if it's already open.

Run VS as admin, and you should be able to go to https://127.0.0.1:portnumber.

抹茶夏天i‖ 2024-10-19 04:56:47

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

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

I was using iisexpress-proxy (from npm) for this.

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

戈亓 2024-10-19 04:56:47

将其留在这里以防万一有人需要...

我需要在 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/

Leaving this here just in case anyone needs...

I needed to have custom domains for a Wordpress Multisite setup in IIS Express but nothing worked until I ran Webmatrix/Visual Studio as an Administrator. Then I was able to bind subdomains to the same application.

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

Then going to http://whatever.localhost:12345/ will run.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文