我在 Docker 容器 (Linux) 的 .NET Core 项目中访问 CSV 文件时遇到问题,它在 Debug模式下工作正常,但在 Release模式下却不行(错误提示找不到文件)。任何想法可能有什么问题?该项目有一个名为“数据”的文件夹,其中包含 CSV 文件。
[Route("GetTestFile")]
[HttpGet]
public IActionResult GetTestFile()
{
var fileName = "testdata.csv";
var filePath = Path.Combine("Data", fileName);
return new FileContentResult(File.ReadAllBytes(filePath), "text/csv") { FileDownloadName = fileName };
}
docker 文件
FROM microsoft/aspnetcore:2.0 AS base
WORKDIR /app
EXPOSE 80
FROM microsoft/aspnetcore-build:2.0 AS build
WORKDIR /src
COPY PVT_Matching_Algorithm/PVT_Matching_Algorithm.csproj PVT_Matching_Algorithm/
RUN dotnet restore PVT_Matching_Algorithm/PVT_Matching_Algorithm.csproj
COPY . .
WORKDIR /src/PVT_Matching_Algorithm
RUN dotnet build PVT_Matching_Algorithm.csproj -c Release -o /app
FROM build AS publish
RUN dotnet publish PVT_Matching_Algorithm.csproj -c Release -o /app
FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "PVT_Matching_Algorithm.dll"]
异常屏幕如下所示:
最佳答案
如果我没看错你的问题。
您必须注入(inject) IHostingEnvironment appEnvironment。
然后你可以这样做:
var filePath = Path.Combine(_appEnvironment.ContentRootPath, "Data/testdata.csv");
关于c# - 在 Docker 容器中访问 .NET Core 项目中的 CSV 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50372648/