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

2

22

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?

Smoot answered 30/1, 2014 at 20:46 Comment(0)
E
4

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

enter image description here

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.

Elba answered 17/6, 2015 at 7:12 Comment(0)
W
3

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>
Wigan answered 2/3, 2014 at 16:1 Comment(2)
This solution does not work in VS2015. After restarting VS2015, the environment overwrites the physicalPath in your copied application section again.Elba
Works in VS2017.Aube

© 2022 - 2024 — McMap. All rights reserved.