[英]How to configure IIS Express to debug an application in a subdirectory?
我的 web 应用程序通常位于 IIS 中根目录的子目录中。假设该子目录是“app”
当我尝试在 Visual Studio 中调试它时,路径始终以本地主机为根,没有子目录。 这弄乱了我在应用程序中的路径。
当我尝试将项目 URL 的形式从http://localhost:port/
更改为http://localhost:port/app
时,它只是说“无法创建虚拟目录”。 嗯,第一次就是这么说的,直到我把application的入口从application.config文件里去掉,然后就成功了。
当我尝试对同一事物使用“覆盖应用程序根 URL”时,它就是行不通。 它要么得到 Error 500.0 Internal Server 错误,要么在某些情况下,它的行为就像是将应用程序的 web.config 文件应用于根目录和虚拟子目录,因为它给出了一个错误,表明我正在尝试添加一个具有已经添加(大概在根目录中)。
我已经尝试在 IIS Express 的 application.config 中编辑项目的设置,但似乎没有任何设置组合起作用。
我不明白为什么 visual studio 将项目放置在 IIS Express 根目录中,而它知道该项目已部署到子目录中的 IIS。 我认为它应该默认匹配目录结构。
这就是现在的样子:
<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>
如果我将应用程序路径更改为“/app”,则会出现错误 500.19,表示配置无效。 如果我做同样的事情并删除虚拟目录标签,那么它就无法启动 IIS Express。
我在这里找到了答案: https : //stackoverflow.com/a/4733387/88409不同的问题,但相同的答案。
基本上,必须创建一个子应用程序,而不仅仅是一个子虚拟目录。
<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>
它似乎需要两个应用程序条目(即它需要一个根应用程序(即使它实际上是一个空的虚拟目录)以及子应用程序)。 如果我省略根目录中的第一个应用程序条目,则会收到错误 500.19“无法访问请求的页面,因为该页面的相关配置数据无效。” 但是,对于这两个应用程序条目,它都可以正常工作。
据我所知,在 Visual Studio 中无法生成上述设置,因此必须在 application.config 中手动编辑。 因此,这些设置与 Web 应用程序项目文件本身完全无关,这并不好,IMO。
好的,我只想为这个问题添加一个更新的答案,因为这是第一个出现在谷歌上的问题,我很难让当前的答案起作用。
您需要在applicationhost.config中更改 3 处才能使其正常工作。
在 VS2022 中生成的配置生成一个带有项目名称的 AppPool(在我的示例中为“ IISVirtualFolderFix AppPool ”)
在配置部分,应该生成一个名称属性与您的项目名称相同的名称(在我的示例中为“ IISVirtualFolderFix ”)。
应该已经有一个应用程序使用当前的应用程序池
<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>
您可以将上面的代码更改为
<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>
现在的问题是,在它起作用之前你还需要做更多的事情。
该站点需要一个根应用程序才能向下导航到子应用程序。 这很容易...只需将 更改为如下所示。
<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>
默认情况下,仅允许在根目录中处理 asp.net 核心,这意味着您必须允许所有子应用程序使用此设置的 inheritance。
应该有一个包含项目文件夹路径的位置部分(在我的示例“ IISVirtualFolderFix ”中,名为 inheritInChildApplications 的属性设置为 false,将其更改为 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>
现在你的子应用程序应该可以工作了。 但是,您仍然会在根目录中开始调试,这也可以修复。
在 Properties/launchSettings.json 中找到 profiles/IIS Express object,并添加 launchUrl 字段。
{
"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"
}
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.