【App Service】在App Service中配置Virtual applications and directories,访问目录中的静态文件报错404 - LuBu0505/My-Code GitHub Wiki

问题描述

在App Service Windows的环境中,想一个App Services下部署多个站点,比如/根目录下,从wwwroot中读取项目文件,而 /files 则通过配置的虚拟目录来读取 \mounts\sitefiles 目录中的静态文件。

在App Service的 Configuration Path Mapping 中配置如下: image.png

通过Kudu查看App Service中的文件列表: image.png

但是,在访问** /files/file1.html** 时,出现404错误 image.png

问题解答

这个问题的根源在于Web.config的配置。因App Service for Windows环境使用IIS作为站点服务器。

如果在 Web.config 文件中,没有指定配置 location 节点,则所有请求都从wwwroot 下查找文件。

查看当前App Service的web.config文件内容,恰好缺少了location节点信息。 image.png

修改web.config,添加    .... ...   后,问题得到解决。image.png

所以,正确的.net 应用部署到App Service后,web.config 的内容应参考下文(location节点不可缺少):

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <location path="." inheritInChildApplications="false">
    <system.webServer>
      <handlers>
        <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModuleV2" resourceType="Unspecified" />
      </handlers>
      <aspNetCore processPath="dotnet" arguments=".\pfxDemo.dll" stdoutLogEnabled="false" stdoutLogFile="\\?\%home%\LogFiles\stdout" hostingModel="inprocess" />
    </system.webServer>
  </location>
</configuration>

参考资料

将 URL 路径映射到目录:https://docs.azure.cn/zh-cn/app-service/configure-common?tabs=portal#map-a-url-path-to-a-directory

Make application and directory-specific configuration settings in an ASP.NET application : https://learn.microsoft.com/zh-cn/troubleshoot/developer/webapps/aspnet/development/application-directory-configuration

当在复杂的环境中面临问题,格物之道需:浊而静之徐清,安以动之徐生。 云中,恰是如此!

⚠️ **GitHub.com Fallback** ⚠️