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

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


当前回答

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

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

其他回答

在我的WebMatrix IIS Express安装中,从“*:80:localhost”更改为“*:80:custom”。hostname“没有工作(“坏的主机名”,即使有适当的etc\hosts映射),但“*:80:”工作-并且没有其他答案所要求的额外步骤。注意,“*:80:*”不会这样做;去掉第二个星号。

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

我需要在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/将运行。

在使用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);只有具有特定主机名的一个就足够了。

并对该方法进行了测试。NET Core 3.1和Visual Studio 2019。

vs PROJECTNAME \配置\ applicationhost。配置

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

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

这两个链接都有效:

https://localhost:44320 https://127.0.0.1:44320

现在,如果你想让应用程序与自定义域一起工作,只需要在主机文件中添加以下一行:

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

127.0.0.1 customdomain

Now:

https://customdomain:44320

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

大卫的解决办法很好。但是我发现页面中的<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,问题就解决了。