在 Docker 容器中访问 .NET Core 项目中的 CSV 文件

ajs*_*117 6 c# docker asp.net-core

我在 Docker 容器 (Linux) 中访问 .NET Core 项目中的 CSV 文件时遇到问题,它在调试模式下工作正常,但在发布模式下工作正常(错误提示找不到文件)。任何想法可能有什么问题?该项目有一个名为“Data”的文件夹,其中包含 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 };
    }
Run Code Online (Sandbox Code Playgroud)

文件

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"]
Run Code Online (Sandbox Code Playgroud)

异常屏幕如下所示:

在此处输入图片说明

Nov*_*kov 0

如果我理解你的问题的话。
您必须注入 IHostingEnvironment appEnvironment。
然后你可以这样做:

var filePath = Path.Combine(_appEnvironment.ContentRootPath, "Data/testdata.csv");
Run Code Online (Sandbox Code Playgroud)