gpt4 book ai didi

c# - docker 无法从 wwwroot 中找到文件

转载 作者:行者123 更新时间:2023-12-05 02:30:40 25 4
gpt4 key购买 nike

这是我第一次尝试在我的 asp.net core 5.0 应用程序中运行 docker 镜像。我在 docker 文件中添加了以下配置。

FROM mcr.microsoft.com/dotnet/aspnet:5.0 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443

FROM mcr.microsoft.com/dotnet/sdk:5.0 AS build
WORKDIR /src
COPY ["Shopping.Client/Shopping.Client.csproj", "Shopping.Client/"]
RUN dotnet restore "Shopping.Client/Shopping.Client.csproj"
COPY . .
WORKDIR "/src/Shopping.Client"
RUN dotnet build "Shopping.Client.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "Shopping.Client.csproj" -c Release -o /app/publish

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "Shopping.Client.dll"]

我正在尝试使用以下代码访问我的 wwwroot 文件夹中的文件:

private async Task<T> ReadAsync<T>(string filePath)
{

using FileStream stream = File.OpenRead(filePath);
return await JsonSerializer.DeserializeAsync<T>(stream);
}

public async Task<List<Product>> GetProducts()
{
var filePath = System.IO.Path.Combine(_env.ContentRootPath, @"wwwroot\Db\product.json");
return await ReadAsync<List<Product>>(filePath);
}

当我运行应用程序并尝试访问 wwwroot 中的文件时,我收到如下错误:

An unhandled exception has occurred while executing the request.
System.IO.FileNotFoundException: Could not find file'/app/wwwroot\Db\product.json'. File name:'/app/wwwroot\Db\product.json'

我是否应该在 docker 文件中包含任何特殊内容以复制 wwwroot 文件夹,或者我是否需要根据 docker 镜像中的路径添加文件路径?

最佳答案

看来您正在使用 Linux 容器并使用 Windows 文件路径约定。 Path.Combine 确实支持这一点,但它需要一些帮助。

/app/wwwroot\Db\product.json

试试 wwwroot/Db/product.json

如果这是您的问题,您可能希望检查 Path.Combine 的重载因为 Combine 方法具有接受额外参数的额外重载

  1. public static string Combine(string path1, string path2, string path3)
  2. public static string Combine(string path1, string path2, string path3, string path4)
  3. public static string Combine(params string[] paths)

您的代码:Path.Combine(_env.ContentRootPath, @"wwwroot\Db\product.json");

然后会变成这样:Path.Combine(basePath, "wwwroot", "Db", "product.json");

关于c# - docker 无法从 wwwroot 中找到文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71783686/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com