ASP.NET核心Docker映像部署/运行成功,但获得404

iswrvxsc  于 2023-03-17  发布在  Docker
关注(0)|答案(1)|浏览(167)

我有一个.Net 6 ASP.NET Web API项目,内置在Windows Docker映像中,并在Azure应用服务中运行(作为Docker容器)。日志显示一切正常。但是,当我尝试访问此Azure Web应用的swagger页面(使用“默认域”)时,我得到了404。我尝试过的示例地址:

为什么无法访问?
再多说一点背景...我的应用成功地作为Windows Docker映像部署到Docker Hub。我的Azure应用服务从Docker Hub获取映像。同样,Visual Studio也可以在Windows Docker桌面中启动相同的Docker映像...并允许我检查Docker容器公开的Swagger页面。
因此,一切看起来“刚刚好”,但我有404时,从Azure应用程序服务。
下面是我正在使用的dockerfile:

#See https://aka.ms/containerfastmode to understand how Visual Studio uses this Dockerfile to build your images for faster debugging.

#Depending on the operating system of the host machines(s) that will build or run the containers, the image specified in the FROM statement may need to be changed.
#For more information, please see https://aka.ms/containercompat

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

FROM mcr.microsoft.com/dotnet/sdk:6.0-windowsservercore-ltsc2022 AS build
WORKDIR /src
COPY ["Alrium.WebAPI/Alrium.WebAPI.csproj", "Alrium.WebAPI/"]
COPY ["Alrium.Data/Alrium.Data.csproj", "Alrium.Data/"]
COPY ["Alrium.Services/Alrium.Services.csproj", "Alrium.Services/"]
COPY ["Alrium.StyleMapper/Alrium.StyleMapper.csproj", "Alrium.StyleMapper/"]
RUN dotnet restore "Alrium.WebAPI/Alrium.WebAPI.csproj"
COPY . .
WORKDIR "/src/Alrium.WebAPI"
RUN dotnet build "Alrium.WebAPI.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "Alrium.WebAPI.csproj" -c Release -o /app/publish /p:UseAppHost=false

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

这个dockerfile是由Visual Studio为我创建的。我唯一更改的是构建映像...我将其更改为Server Core(默认为Server Nano)。我之所以进行此更改,是因为Microsoft表示使用Server Core可以规避构建问题。
有什么想法吗?

vjhs03f7

vjhs03f71#

根据您的描述,您可以通过注解Isdevelopment条件来修复此问题。代码如下所示。

var app = builder.Build();

        // Configure the HTTP request pipeline.

        /// ====>comment this condition app.Environment.IsDevelopment())<===========
        //if (app.Environment.IsDevelopment())
        //{
        //    app.UseSwagger();
        //    app.UseSwaggerUI();
        //}

        app.UseSwagger();
        app.UseSwaggerUI(c =>
        {
            c.SwaggerEndpoint("/swagger/v1/swagger.json", "My Api v1");
            c.RoutePrefix = string.Empty;
        });

        app.UseHttpsRedirection();

        app.UseAuthorization();

        app.MapControllers();

        app.Run();

相关问题