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

标签 c# docker asp.net-core

我在 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"]

异常屏幕如下所示:

enter image description here

最佳答案

如果我没看错你的问题。
您必须注入(inject) IHostingEnvironment appEnvironment。
然后你可以这样做:

var filePath = Path.Combine(_appEnvironment.ContentRootPath, "Data/testdata.csv");

关于c# - 在 Docker 容器中访问 .NET Core 项目中的 CSV 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50372648/

相关文章:

c# - 使用 LINQ 生成素数

javascript - 使用 Internet Explorer 的 JS 中的日期时间字符串中不需要的项目符号/点

c# - 在 SQL Server Compact Edition 中将 Entity Framework 代码中的种子主键首先设置为 0

docker - Curl命令的基本身份验证成为openshift的 secret

Docker-compose:用生产中预构建的镜像替换基于 "build"的服务?

Docker 最佳实践 : use OS or application as base image?

c# - 同一类型参数的协变和逆变

asp.net-core - 对预检请求的响应未通过访问控制检查 : It does not have HTTP ok status. GET 工作 POST PUT DELETE 不工作

asp.net - 有没有办法在 .NET Core 1.0 MVC 的 View 中使用授权策略?

asp.net - 在应用程序启动时使用 Entity Framework ASP.NET Core 运行数据库迁移