How do I prevent Visual Studio from modifying the IIS Express site's phyical path in applicationhost.config?

元气小坏坏 提交于 2019-12-06 18:33:21

问题


I have a Visual Studio web application project that, for certain reasons, copies files from multiple projects to a separate output directory. I want to use this output directory as the root of the associated IIS Express site. In IIS Express' applicationhost.config file, I can set the associated site's physical path to the correct directory. I'll set it like this:

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

However, when I reopen the project, Visual Studio overwrites my specified physical path, reverting it to the project's own directory. Even worse, Visual Studio gives me no indication that it has done this. Here's how the <virtualDirectory> element looks after Visual Studio messes it up:

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

How can I prevent Visual Studio from overwriting this path?


回答1:


Visual Studio 2013 and 2015 does not change the physical path for the option 'Override applicationpool URL':

The file %userprofile%\documents\iisexpress\config\applicationhost.config looks like the following as default:

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

Just copy the your default block above, paste it directly below and make some changes. Change the name, id, physicalPath and override the URL with the additional subdomain. In my case 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>

Now, when I start VS and run the IIS Express, Visual studio does not change the physicalPath in the applicationhost.config of the overwritten URL. That works for me.

Hint for Visual Studio 2015: Visual Studio 2015 uses the file YourProject/.vs/config/applicationhost.config and overrides it every time you open the environment. Open your *.proj file and set the following entry:

<UseGlobalApplicationHostFile>true</UseGlobalApplicationHostFile>

With this configuration, the IIS Express uses your global application host file located at: %userprofile%\documents\iisexpress\config\applicationhost.config.




回答2:


I wasn't able to prevent VS to override the physicalPath for MySiteName but as a workaround I added another application section with different path (lets say "NewPath") and didn't update VS to use this path under the csproj web properties. In this case when debugging it will automatically open the browser on the old url (http://localhost:63470/) if you navigate to the new endpoint (http://localhost:63470/NewPath) everything will work fine and VS will not revert this.

So the new configuration looks like this:

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


来源:https://stackoverflow.com/questions/21467083/how-do-i-prevent-visual-studio-from-modifying-the-iis-express-sites-phyical-pat

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!