c# - 带有 Docker 的 HTTP 错误 404 dotnet 核心 API

标签 c# .net docker asp.net-core .net-core

我正在尝试在 docker 容器内运行 dotnet Web API,但是当我运行它时,我无法导航到该站点,我收到 HTTP ERROR 404在浏览器中。

这是我的 dockerfile:

FROM mcr.microsoft.com/dotnet/core/aspnet:2.2-stretch-slim AS base
WORKDIR /app

FROM mcr.microsoft.com/dotnet/core/sdk:2.2-stretch AS build
WORKDIR /src
COPY Backend/AppManagerAPI/AppManagerAPI.csproj Backend/AppManagerAPI/
COPY Backend/DTO/DTO.csproj Backend/DTO/
COPY nuget.config ./
COPY Build/dependencies.props Build/
COPY Directory.Build.props ./
COPY ./*.sln ./
COPY Backend/Database/Database.csproj Backend/Database/
COPY Backend/Service/Service.csproj Backend/Service/
COPY Utilities/Utilities.csproj Utilities/
RUN dotnet restore "Backend/AppManagerAPI/AppManagerAPI.csproj"
COPY . .
WORKDIR "/src/Backend/AppManagerAPI"
RUN dotnet build "AppManagerAPI.csproj" -c Release -o /app

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

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

我加了 .UseUrls("http://*:5000")所以我在 program.cs 上监听来自端口 5000 的每个调用:

public class Program
    {
        public static void Main(string[] args)
        {
            CreateWebHostBuilder(args).Build().Run();
        }

        public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
                .UseStartup<Startup>()
                .UseUrls("http://*:5000");
    }

如果我运行 docker logs appmanager我明白了:
Hosting environment: Production
Content root path: /app
Now listening on: http://[::]:5000
Application started. Press Ctrl+C to shut down.

这是我运行容器的方式:docker run -d -p 5000:5000 --name appmanager appmanager
我已经尝试了一切,但我不知道我错过了什么......

最佳答案

发现问题 - 问题出在 csproj 设置文件上。

删除所有 Microsoft.* PackageReferences 并仅添加以下一项后:

<PackageReference Include="Microsoft.AspNetCore.App" />

一切都按预期开始工作! :)

关于c# - 带有 Docker 的 HTTP 错误 404 dotnet 核心 API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57729627/

相关文章:

c# - 是否有内置方法将 HRESULT 表示为 winerror.h 常量(例如 E_FAIL)?

bash - Docker Shell脚本未按预期参数化

docker - 错误响应 : Could not kill running container runc did not terminate sucessfully: container_linux. go:392: init process caused "permission denied"

c# - 如何修复 : NullReferenceException: Do not create your own module instances, 从 ParticleSystem 实例获取它们

c# - 引用自动实现属性的支持字段

.net - 自动映射器 : mapping issue with inheritance and abstract base class on collections with Entity Framework 4 Proxy Pocos

Docker - 挂载目录的所有者和组

c# - 在 Azure Function 中使用新的 SaaS 表适配器

c# - 通过依赖注入(inject)创建 AntiForgeryToken

c# - 如何在 WPF 的 Combobox 中有效地添加多个项目