3
votes

Swagger UI is not creating in a .net core application when deployed in azure but it is working perfectly in local

I have added this in

ConfigureServices(IServiceCollection services) methode in startup.cs

services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new Info
{
Version = "v1",
Title = "API",
Description = "API"
});
});

and

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

env.ConfigureNLog("nlog.config");
loggerFactory.AddNLog();

app.UseSignalR(routes =>
{
  routes.MapHub<DashboardHub>("/hubs/dashboard");
});

app.UseMvc(routes =>
{
   routes.MapRoute(
   name: "default",
   template: "{controller}/{action=Index}/{id?}");
});

app.UseSpa(spa =>
{
    spa.Options.SourcePath = "ClientApp";

    if (env.IsDevelopment())
    {
         spa.UseReactDevelopmentServer(npmScript: "start");
    }
});

in Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)

2
Which error do you get? And how do you get it? - user743414
No error is getting and no json file is generated in the path {baseurl}/swagger/v1/swagger.json - Akhil
have you tried c.RoutePrefix = string.empty? - SWilko
@SWilko Tried with c.RoutePrefix = string.Empty , but same issue not getting the UI page.Does this due to any configuration in azure? - Akhil

2 Answers

4
votes

Make sure the code isn't in IF conditional (env.IsDevelopment()). This the reason why only work on localhost. This avoids working in production time.

Take out the swagger initialization like the example.

            if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();

            // NOT HERE

        }

        // Enable middleware to serve generated Swagger as a JSON endpoint.  
        app.UseSwagger();

        // Enable middleware to serve swagger-ui (HTML, JS, CSS, etc.),  
        // specifying the Swagger JSON endpoint.  
        app.UseSwaggerUI(c =>
        {
            c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");
            c.RoutePrefix = string.Empty;
        });
0
votes

Couple of things to look at:

  • Please ensure you are using the right and upgraded stable version, You can refer below screenshot for verifying:

enter image description here

  • Use the http://www.test-cors.org website to verify CORS support. Keep in mind this will show a successful result even if Access-Control-Allow-Headers is not available, which is still required for Swagger UI to function properly.

  • Please ensure that you have followed up below doc for configuring swagger with dot net core.

https://github.com/domaindrivendev/Swashbuckle.AspNetCore#swashbuckleaspnetcoreswaggerui

If still doesn't help, please provide the code repo, will help you further.

Note: swagger-ui-react is Swagger UI packaged as a React component for use in React applications.

Also you cane browse through some samples here:

https://github.com/domaindrivendev/Swashbuckle.AspNetCore/blob/master/test/WebSites/NetCore3/Startup.cs

Hope it helps.