简体   繁体   中英

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM