8

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)

4
  • 1
    Which error do you get? And how do you get it? Commented Aug 27, 2019 at 12:15
  • 1
    No error is getting and no json file is generated in the path {baseurl}/swagger/v1/swagger.json Commented Aug 27, 2019 at 12:37
  • have you tried c.RoutePrefix = string.empty? Commented Aug 27, 2019 at 16:56
  • @SWilko Tried with c.RoutePrefix = string.Empty , but same issue not getting the UI page.Does this due to any configuration in azure? Commented Aug 28, 2019 at 11:58

2 Answers 2

18

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;
        });
Sign up to request clarification or add additional context in comments.

3 Comments

this worked for me. Now I am able to see my swagger.json file on my Azure subdomain instead of locally only. I wonder why the default was set locally only.
Thanks it works. But can you briefly explain why env.IsDevelopment() is not true? The environment variable ASPNETCORE_ENVIRONMENT is Development.
@anhtv13 - use DOTNET_ENVIRONMENT env. variable when assigning env to web api app. it should work with IsDevelopment(), IsStaging() and IsProduction() out of the box.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.