ASP .NET Core:Directory.EnumerateFiles 和 IIS 托管

ASP .NET Core : Directory.EnumerateFiles and IIS hosting

当我在 IIS 服务器上发布我的 ASP .NET Core (v.2.2) Web 应用程序时,它在这一行抛出异常:

Directory.EnumerateFiles(_environment.ContentRootPath + @"/Pages/API")

异常:

An unhandled exception has occurred while executing the request. Could not find a part of the path 'C:\Release\MySite\Pages\API'. System.IO.DirectoryNotFoundException at System.IO.Enumeration.FileSystemEnumerator`1.CreateDirectoryHandle(String path, Boolean ignoreNotFound)

当我查看已发布的文件夹时,没有 API 文件夹,但它不应该在我的网站 dll 中吗?或者我在 ISS 上发布 Web 项目时是否可以不使用相对或绝对路径来查找文件?

注意:文件夹 API 中的页面具有 Build Action : 内容,并且代码在开发中没有问题(使用 IIS-express)。

对于 asp.net 核心,它将在发布到 Project.Views.dll 时预编译视图。对于Directory.EnumerateFiles,它只列出磁盘中真实存在的文件。

如需解决方案,请尝试修改您的 project.csproj 以添加 <MvcRazorCompileOnPublish>false</MvcRazorCompileOnPublish>

已满

<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
    <TargetFramework>netcoreapp2.2</TargetFramework>
    <AspNetCoreHostingModel>InProcess</AspNetCoreHostingModel>
    <MvcRazorCompileOnPublish>false</MvcRazorCompileOnPublish>
</PropertyGroup>


<ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.App" />
    <PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.2.0" PrivateAssets="All" />
    <PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.2.0" />
</ItemGroup>
</Project>