简体   繁体   中英

How to configure IIS Express to debug an application in a subdirectory?

My web application is typically located in a subdirectory of the root in IIS. Suppose the subdirectory is "app"

When I try to debug it in Visual Studio, the path is always rooted at localhost, with no subdirectory. This messes up my paths in the application.

When I try to change the project URL's form from http://localhost:port/ to http://localhost:port/app it just says "Cannot create virtual directory." Well, that's what it said the first time, until I removed the application entry from the application.config file, then it succeeded.

When I try the "override application root URL" to the same thing, it just doesn't work. It either gets an Error 500.0 Internal Server error, or in some cases it acts like it's applying the application's web.config file to both the root and the virtual subdirectory, because it gives an error that I'm trying to add a module that has already been added (presumably in the root).

I've tried editing the project's settings in application.config for IIS Express, but no combination of settings seems to work.

I don't understand why visual studio is situating the project at an IIS Express root directory, when it knows this project is deployed to IIS in a subdirectory. I think it should match the directory structure by default.

This is what it looks like now:

<site name="app" id="5">
   <application path="/" applicationPool="Clr4IntegratedAppPool">
     <virtualDirectory path="/app" physicalPath="C:\Users\username\Desktop\wwwroot\app" />
   </application>
   <bindings>
     <binding protocol="http" bindingInformation="*:3056:localhost" />
   </bindings>
</site>

If I change the application path to "/app", then it gives error 500.19, saying the configuration is invalid. If I do the same thing and remove the virtual directory tag, then it fails to launch IIS Express altogether.

I found the answer here: https://stackoverflow.com/a/4733387/88409 Different question, but same answer.

Basically, one must create a child application, not just a child virtual directory.

<site name="app" id="5">
    <application path="/" applicationPool="Clr4IntegratedAppPool">
        <virtualDirectory path="/" physicalPath="C:\Users\username\Desktop\wwwroot" />
    </application>
    <application path="/app" applicationPool="Clr4IntegratedAppPool">
      <virtualDirectory path="/" physicalPath="C:\Users\username\Desktop\wwwroot\app" />
    </application>
    <bindings>
        <binding protocol="http" bindingInformation="*:3056:localhost" />
    </bindings>
</site>

It seems to require both application entries (ie it requires a root application (even if it's physically an empty dummy directory) as well as the child application). If I omit the first application entry at the root, then it gets error 500.19 "The requested page cannot be accessed because the related configuration data for the page is invalid." With both application entries, however, it works without any issues.

As far as I can tell, there is no way to produce the above settings in Visual Studio, so it must be edited manually in application.config. These settings are therefore completely disassociated with the web application project files themselves, which isn't good, IMO.

Okay, I just want to add an updated answer to this question, as it is the first one coming up on google, and I was having a hard time getting the current answer to work.

there are 3 things you need to change in the applicationhost.config to make it work.

the config generated in VS2022 generates an AppPool with the name of the Project (in my example " IISVirtualFolderFix AppPool ")

step 1) change the path of the application

in the section of the config, there should be a generated with the name attribute named the same as your project (in my example " IISVirtualFolderFix ").

there should already be an application using the current application pool

<site name="IISVirtualFolderFix" id="2">
    <application path="/" applicationPool="IISVirtualFolderFix AppPool">
        <virtualDirectory path="/" physicalPath="E:\Codes\Experiments\AspNetCore\IISVirtualFolderFix\IISVirtualFolderFix" />
    </application>
    <bindings>
      <binding protocol="http" bindingInformation="*:54180:localhost" />
      <binding protocol="https" bindingInformation="*:44381:localhost" />
    </bindings>
</site>

you can change the above code to

<site name="IISVirtualFolderFix" id="2">
    <application path="/" applicationPool="IISVirtualFolderFix AppPool">
        <virtualDirectory path="/sub" physicalPath="E:\Codes\Experiments\AspNetCore\IISVirtualFolderFix\IISVirtualFolderFix" />
    </application>
    <bindings>
      <binding protocol="http" bindingInformation="*:54180:localhost" />
      <binding protocol="https" bindingInformation="*:44381:localhost" />
    </bindings>
</site>

now the problem is, that you still need to do more before it works.

step 2) add root application

the site needs a root application before it can navigate down to the children. this is very easy... simply change the to look like this.

<site name="IISVirtualFolderFix" id="2">
    <application path="/"></application>
    <application path="/sub" applicationPool="IISVirtualFolderFix AppPool">
        <virtualDirectory path="/" physicalPath="E:\Codes\Experiments\AspNetCore\IISVirtualFolderFix\IISVirtualFolderFix" />
     </application>
     <bindings>
         <binding protocol="http" bindingInformation="*:54180:localhost" />
         <binding protocol="https" bindingInformation="*:44381:localhost" />
     </bindings>
</site>

step 3) allow processing of as.netcore in sub-applications

as default, the processing of the asp.net core is only permitted in the root-dir, meaning you have to allow inheritance of this setting to all sub-applications.

there should be a location section with a path to the project folder (in my example " IISVirtualFolderFix " with an attribute named inheritInChildApplications set to false, change that to true.

<location path="IISVirtualFolderFix" inheritInChildApplications="true">
    <system.webServer>
      <modules>
        <remove name="WebMatrixSupportModule" />
      </modules>
      <handlers>
        <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModuleV2" resourceType="Unspecified" />
      </handlers>
      <aspNetCore processPath="%LAUNCHER_PATH%" stdoutLogEnabled="false" hostingModel="InProcess" startupTimeLimit="3600" requestTimeout="23:00:00" />
      <httpCompression>
        <dynamicTypes>
          <add mimeType="text/event-stream" enabled="false" />
        </dynamicTypes>
      </httpCompression>
    </system.webServer>
  </location>

All done.

now your sub-application should work. however, you will still start debugging in the root dir, and that can also be fixed.

in the Properties/launchSettings.json find the profiles/IIS Express object, and add the launchUrl field.

{
  "iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iisExpress": {
      "applicationUrl": "http://localhost:54180",
      "sslPort": 44381
    }
  },
  "profiles": {
    "IISVirtualFolderFix": {
      "commandName": "Project",
      "dotnetRunMessages": true,
      "launchBrowser": true,
      "applicationUrl": "https://localhost:7247;http://localhost:5247",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "IIS Express": {
      "commandName": "IISExpress",
      "launchUrl": "sub",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    }
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM