Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET Core - Swashbuckle not creating swagger.json file

I am having trouble getting the Swashbuckle.AspNetCore (1.0.0) package to generate any output. I read the swagger.json file should be written to '~/swagger/docs/v1'. However, I am not getting any output.

I started with a brand new ASP.NET Core API project. I should mention this is ASP.NET Core 2. The API works, and I am able to retrieve values from the values controller just fine.

My startup class has the configuration exactly as described in this article (Swashbuckle.AspNetCore on GitHub).

public class Startup {     public Startup(IConfiguration configuration)     {         Configuration = configuration;     }      public IConfiguration Configuration { get; }      // This method gets called by the runtime. Use this method to add services to the container.     public void ConfigureServices(IServiceCollection services)     {         services.AddMvc();          services.AddSwaggerGen(c =>         {             c.SwaggerDoc("v1", new Info { Title = "My API", Version = "v1" });         });     }      // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.     public void Configure(IApplicationBuilder app, IHostingEnvironment env)     {         if (env.IsDevelopment())         {             app.UseDeveloperExceptionPage();              // Enable middleware to serve generated Swagger as a JSON endpoint.             app.UseSwagger();             app.UseSwaggerUI(c =>             {                 c.SwaggerEndpoint("/swagger/v1/swagger.json", "MyAPI V1");             });         }         else         {             app.UseExceptionHandler();         }          app.UseStatusCodePages();         app.UseMvc();          //throw new Exception();     } } 

You can see the NuGet references...

enter image description here

Again, this is all the default template, but I include the ValuesController for reference...

[Route("api/[controller]")] public class ValuesController : Controller {     // GET api/values     [HttpGet]     public IEnumerable<string> Get()     {         return new string[] { "value1", "value2" };     }      // GET api/values/5     [HttpGet("{id}")]     public string Get(int id)     {         return "value";     }      // POST api/values     [HttpPost]     public void Post([FromBody]string value)     {     }      // PUT api/values/5     [HttpPut("{id}")]     public void Put(int id, [FromBody]string value)     {     }      // DELETE api/values/5     [HttpDelete("{id}")]     public void Delete(int id)     {     } } 
like image 955
John Livermore Avatar asked Jan 25 '18 19:01

John Livermore


People also ask

How do I enable Swagger in .NET Core?

Add and configure Swagger middlewareLaunch the app and navigate to https://localhost:<port>/swagger/v1/swagger.json . The generated document describing the endpoints appears as shown in OpenAPI specification (openapi. json). The Swagger UI can be found at https://localhost:<port>/swagger .

Where is Swagger v1 Swagger json?

Launch the app, and navigate to http://localhost:<port>/swagger/v1/swagger.json . The generated document describing the endpoints appears as shown in Swagger specification (swagger. json). The Swagger UI can be found at http://localhost:<port>/swagger .


2 Answers

I had the same problem. Check http://localhost:XXXX/swagger/v1/swagger.json. If you get any a errors, fix them.

For example, I had an ambiguous route in a base controller class and I got the error: "Ambiguous HTTP method for action. Actions require an explicit HttpMethod binding for Swagger 2.0.". If you use base controllers make sure your public methods use the HttpGet/HttpPost/HttpPut/HttpDelete OR Route attributes to avoid ambiguous routes.

Then, also, I had defined both HttpGet("route") AND Route("route") attributes in the same method, which was the last issue for swagger.

like image 181
Marisol Gutiérrez Avatar answered Oct 13 '22 21:10

Marisol Gutiérrez


I believe you missed these two lines on your Configure:

if (env.IsDevelopment()) {     app.UseDeveloperExceptionPage();      // Enable middleware to serve generated Swagger as a JSON endpoint.     app.UseSwagger();     app.UseSwaggerUI(c =>     {         c.SwaggerEndpoint("v1/swagger.json", "MyAPI V1");     }); } 

To access Swagger UI, the URL should be: http://localhost:XXXX/swagger/

The json can be found at the top of Swagger UI:

enter image description here

like image 22
Tiago B Avatar answered Oct 13 '22 21:10

Tiago B