Swagger page load empty index html page in docker

5cnsuln7  于 2023-10-18  发布在  Docker
关注(0)|答案(1)|浏览(134)

当我点击docker桌面镜像3004:80链接时,它显示一个空的http://localhost:3004/index.html。我需要手动输入http://localhost:3004/swagger/index.html,然后它将加载swagger页面。
如何在加载时默认加载swagger索引页面?谢谢
这里是docker文件

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

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

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

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

下面是program.cs文件

builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(c =>
{
    c.SwaggerDoc("v1", new Microsoft.OpenApi.Models.OpenApiInfo
    {
        Title = "My Web API",
        Version = "v1",
        Description = "ASP.NET Core 6 Web API",
        Contact = new Microsoft.OpenApi.Models.OpenApiContact
        {
            Name = "Demo",
            Email = "[email protected]"
        }
    });
    var xmlFile = $"{System.Reflection.Assembly.GetExecutingAssembly().GetName().Name}.xml";
    var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
    c.IncludeXmlComments(xmlPath);
});

app.UseSwagger();
app.UseSwaggerUI();

下面是launchSettings.json

{
  "profiles": {
    "urlShortener": {
        "commandName": "Project",
        "launchBrowser": true,
        "launchUrl": "swagger",
        "environmentVariables": {
            "ASPNETCORE_ENVIRONMENT": "Development"
        },
        "dotnetRunMessages": true,
        "applicationUrl": "http://localhost:5000"
    },
    "IIS Express": {
        "commandName": "IISExpress",
        "launchBrowser": true,
        "launchUrl": "swagger",
        "environmentVariables": {
            "ASPNETCORE_ENVIRONMENT": "Development"
        }
    },
    "Docker": {
        "commandName": "Docker",
        "launchBrowser": true,
        "launchUrl": "{Scheme}://{ServiceHost}:{ServicePort}/",
        "environmentVariables": {
            "ASPNETCORE_URLS": "http://+:80/"
        },
        "publishAllPorts": true
    }
  },
  "$schema": "https://json.schemastore.org/launchsettings.json",
  "iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iisExpress": {
      "applicationUrl": "http://localhost:64900",
      "sslPort": 0
    }
  }
}
nlejzf6q

nlejzf6q1#

您可以更改launchURL

"Docker": {
        "commandName": "Docker",
        "launchBrowser": true,
        "launchUrl": "{Scheme}://{ServiceHost}:{ServicePort}/swagger/index.html",
        ...
  • 更新-
    强制重定向就行了。
app.MapControllers();
app.MapGet("/", () => Results.Redirect("/swagger", true, true));

相关问题