【问题标题】:How do I prevent Visual Studio from modifying the IIS Express site's phyical path in applicationhost.config?如何防止 Visual Studio 修改 applicationhost.config 中的 IIS Express 站点物理路径?
【发布时间】:2014-01-30 20:46:21
【问题描述】:

我有一个 Visual Studio Web 应用程序项目,由于某些原因,它会将文件从多个项目复制到单独的输出目录。我想将此输出目录用作关联 IIS Express 站点的根目录。在 IIS Express 的 applicationhost.config 文件中,我可以将关联站点的物理路径设置为正确的目录。我会这样设置:

<site name="MySiteName" id="42">
    <application path="/" applicationPool="Clr4IntegratedAppPool">
        <virtualDirectory path="/" physicalPath="c:\my\desired\path" />
    </application>
    <bindings>
        <binding protocol="http" bindingInformation="*:63470:localhost" />
    </bindings>
</site>

但是,当我重新打开项目时,Visual Studio 会覆盖我指定的物理路径,将其恢复到项目自己的目录。更糟糕的是,Visual Studio 没有给我任何迹象表明它已经这样做了。下面是 &lt;virtualDirectory&gt; 元素在 Visual Studio 搞砸后的样子:

        <virtualDirectory path="/" physicalPath="c:\path\to\project" />

如何防止 Visual Studio 覆盖此路径?

【问题讨论】:

    标签: visual-studio iis-express applicationhost


    【解决方案1】:

    Visual Studio 2013 和 2015 不会更改选项“覆盖应用程序池 URL”的物理路径:

    %userprofile%\documents\iisexpress\config\applicationhost.config 文件默认如下所示:

    <site name="MyWebSite" id="1477659296">
        <application path="/" applicationPool="Clr4IntegratedAppPool">
            <virtualDirectory path="/" physicalPath="C:\Projects\MyWebSite" />
        </application>
        <bindings>
            <binding protocol="http" bindingInformation="*:62238:localhost" />
            <binding protocol="http" bindingInformation="*:62238:*" />
        </bindings>
    </site>
    

    只需复制上面的默认块,将其直接粘贴到下面并进行一些更改。更改 nameidphysicalPath 并使用附加子域覆盖 URL。在我的情况下debug

    <site name="MyWebSiteOverwritten" id="99999999">
        <application path="/" applicationPool="Clr4IntegratedAppPool">
            <virtualDirectory path="/" physicalPath="C:\Projects\DifferentPath" />
        </application>
        <bindings>
            <binding protocol="http" bindingInformation="*:62238:debug.localhost" />
        </bindings>
    </site>
    

    现在,当我启动 VS 并运行 IIS Express 时,Visual Studio 不会更改覆盖 URL 的 applicationhost.config 中的物理路径。这对我有用。

    Visual Studio 2015 提示:Visual Studio 2015 使用文件YourProject/.vs/config/applicationhost.config 并在您每次打开环境时覆盖它。打开您的*.proj 文件并设置以下条目:

    <UseGlobalApplicationHostFile>true</UseGlobalApplicationHostFile>
    

    通过此配置,IIS Express 使用您位于以下位置的全局应用程序主机文件:%userprofile%\documents\iisexpress\config\applicationhost.config

    【讨论】:

      【解决方案2】:

      我无法阻止 VS 覆盖 MySiteName 的物理路径,但作为一种解决方法,我添加了另一个具有不同路径的应用程序部分(比如说“NewPath”)并且没有更新 VS 以在 csproj 网络下使用此路径特性。在这种情况下,如果您导航到新的端点 (http://localhost:63470/NewPath),它会在调试时自动在旧 url (http://localhost:63470/) 上打开浏览器,一切都会正常工作,VS 不会恢复。

      所以新的配置是这样的:

      <site name="MySiteName" id="42">
           <application path="/" applicationPool="Clr4IntegratedAppPool">
               <virtualDirectory path="/" physicalPath="c:\path\to\project" />
           </application>
           <application path="/NewPath" applicationPool="Clr4IntegratedAppPool">
               <virtualDirectory path="/" physicalPath="c:\my\desired\path" />
           </application>
           <bindings>
               <binding protocol="http" bindingInformation="*:63470:localhost" />
           </bindings>
      </site>
      

      【讨论】:

      • 此解决方案在 VS2015 中不起作用。重启VS2015后,环境再次覆盖了你复制的应用部分的物理路径。
      • 适用于 VS2017。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多