Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

1.7K
Visualizações
Fetching swagger-ui returns 404 in a ASP.NET Core 3.1 app in Azure

When loading the /swagger/index.html page the browser can't find the swagger-ui resources required when deployed to an App Servicce in Azure, returning 404. It works when running locally. My setup is:

  services.AddSwaggerGen(options =>
  {
      options.SwaggerDoc("v1", new OpenApiInfo
      {
          Title = "Nexus WebApp",
          Version = "v1"
      });
      options.CustomSchemaIds(type => type.ToString());
               
  });
 var builder = endpoints.CreateApplicationBuilder();

 builder.UseSwagger();

 builder.UseSwaggerUI(options =>
 {
     options.SwaggerEndpoint("/swagger/v1/swagger.json", "Nexus WebApp");
 });

 var pipeline = builder.Build();

 endpoints.Map("/swagger", pipeline)
     .RequireAuthorization(new AuthorizeAttribute());
 endpoints.Map("/swagger/index.html", pipeline)
     .RequireAuthorization(new AuthorizeAttribute());
 endpoints.Map("/swagger/{documentName}/swagger.json", pipeline)
     .RequireAuthorization(new AuthorizeAttribute());

I've tried with a relative path fro the swagger endpoint like so ..\swagger\v1/swagger.json, I've tried specifying a RoutePrefix all to no avail unfortunately. I'm aware similar questions have been asked but unfortunately none seem to help.

Does anyone have any clues?

Update

These are the resources it is returning 404 for:

  • https://{domain}/swagger/swagger-ui.css
  • https://{domain}/swagger/swagger-ui-standalone-preset.js
  • https://{domain}/swagger/swagger-ui-bundle.js
over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

I suspect, your implementation is wrong. Here is the complete Startup code for setting up Swagger UI in NET Core:

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Swashbuckle.AspNetCore.Swagger;
namespace IDGSwaggerDemo
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }
        public IConfiguration Configuration { get; }
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc().SetCompatibilityVersion
            (CompatibilityVersion.Version_2_2);   
            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", new Info
                {
                    Version = "v1",
                    Title = "Swagger Demo",
                    Description = "Swagger Demo for ValuesController",
                    TermsOfService = "None",
                    Contact = new Contact() { Name = "Joydip Kanjilal",
                    Email = "joydipkanjilal@yahoo.com",
                    Url = "www.google.com"
                }
                });
            });
        }
        public void Configure(IApplicationBuilder app,
       IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            app.UseMvc();
            app.UseSwagger();
            app.UseSwaggerUI(c =>
            {
                c.SwaggerEndpoint("/swagger/v1/swagger.json", "v1");
            });
        }
    }
}

Check out this tutorial for more enhanced Swagger implementation.

over 4 years ago · Santiago Trujillo Relatório

0

Finally, after struggling with this for hours, I managed to get it to work. Note that I have the special requirement that I have a Web API for which I want controllers to use Bearer token authentication, and at the same time I want the Swagger UI to be protected by OIDC.

I have left out parts in the class that are not relevant for this answer.

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddMvc();

// Add authentication to web API
builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
    .AddMicrosoftIdentityWebApi(builder.Configuration);

// Add authentication to web app for Swagger authorization
builder.Services.AddAuthentication(OpenIdConnectDefaults.AuthenticationScheme)
    .AddMicrosoftIdentityWebApp(builder.Configuration);

builder.Services.AddSwaggerGen();

builder.Services.AddAuthorization(options =>
{
    options.DefaultPolicy = new AuthorizationPolicyBuilder(JwtBearerDefaults.AuthenticationScheme)
        .RequireAuthenticatedUser()
        .Build();
});


var app = builder.Build();

app.UseAuthentication();

app.UseAuthorization();

app.Use(async (context, next) =>
{
    if (context.Request.Path.StartsWithSegments("/swagger") && context.User.Identity?.IsAuthenticated == false)
    {
        await context.ChallengeAsync(OpenIdConnectDefaults.AuthenticationScheme);
    }
    else
    {
        await next();
    }
});

app.UseSwagger();

app.UseSwaggerUI(c =>
{
    c.SwaggerEndpoint("/swagger/v1/swagger.json", "API v1");
});

app.MapControllers();

app.Run();
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda