【问题标题】:Docker container is not loading wwwroot content Dotnet Core 3.1Docker 容器未加载 wwwroot 内容 Dotnet Core 3.1
【发布时间】:2020-07-16 02:18:01
【问题描述】:

我有一个基于分层的应用程序,包括 API、业务、通用、域和基础架构层。 API 层有一个 wwwroot 文件夹,其中包含 Angular 构建内容。

在配置管道的启动时,我启用了 UseStaticFiles()。

当我在本地运行时,使用

donet 观看运行

应用程序运行成功。

接下来,我构建 docker 文件。 docker文件放在.sln所在的目录中。

这里是docker文件的内容。

FROM mcr.microsoft.com/dotnet/core/sdk:3.1 AS build

# Copy csproj and restore as distinct layers
COPY *.sln /build/
COPY Admin.Core/*.csproj ./build/Admin.Core/
COPY Admin.Business.Common/*.csproj ./build/Admin.Business.Common/
COPY Admin.Infrastructure/*.csproj ./build/Admin.Infrastructure/
COPY Admin.Business/*.csproj ./build/Admin.Business/
COPY Admin.API/*.csproj ./build/Admin.API/

# COPY *.csproj /build/
RUN dotnet restore ./build/Admin.API/Admin.API.csproj

# Copy everything else and build
COPY . ./build/

WORKDIR /build
RUN dotnet publish ./Admin.API/Admin.API.csproj -c Release -o out 

# Build runtime image
FROM mcr.microsoft.com/dotnet/core/aspnet:3.1 AS runtime

WORKDIR /app
COPY --from=build /build/out .

ENTRYPOINT ["dotnet", "Admin.API.dll"]

当我构建镜像并在容器中运行时,应用程序运行成功,我可以使用 postman 成功调用 API。但在浏览器中,未加载 wwwroot 内容。 该文件夹存在于容器文件中。 我该如何解决这个问题?

【问题讨论】:

    标签: docker .net-core dockerfile


    【解决方案1】:

    这不是码头工人的问题。这是 .NET Core 中对中间件的错误使用。

    静态文件应该在路由之前添加。

        app.UseStaticFiles();
        .
        .
        .
        app.UseRouting();
    

    添加中间件的顺序非常重要。因为UseRouting 是在UseStaticFiles 之前添加的,所以MVC 路由正在寻找匹配MVC 规则的图像和css 文件,这当然不可用。因此可能找不到这些。

    更改顺序将解决它。

    【讨论】:

    • 感谢您的回复。其实我昨天做了这个。但结果还是一样。
    • @arslanMunir 你解决了吗?请告诉我我有同样的问题;(
    猜你喜欢
    • 2021-06-25
    • 2020-10-29
    • 2020-11-27
    • 2020-10-10
    • 2018-08-15
    • 2019-08-28
    • 1970-01-01
    • 1970-01-01
    • 2020-11-27
    相关资源
    最近更新 更多