Akhil
Akhil

Reputation: 81

Swagger UI not generating in azure .net core but it is working in local

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)

Upvotes: 8

Views: 7671

Answers (2)

Eduardo Albarado
Eduardo Albarado

Reputation: 191

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;
        });

Upvotes: 18

Mohit Verma
Mohit Verma

Reputation: 5294

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.

Upvotes: 0

Related Questions