Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change location of swagger JSON using Swashbuckle

I am trying to configure Swashbuckle so the generated JSON file can be accessed using the URL {root}/swagger.json.

I've manipulated a number of settings but have been unable to get it to work. Here are some examples:

// This works!  JSON file is located at http://{root}/swagger/docs/v1
this.EnableSwagger(c =>
{
    c.RootUrl(x => baseUrl);
    c.SingleApiVersion("v1", title);
}).EnableSwaggerUi();

This works!  JSON file is located at http://{root}/swagger/docs/swagger
this.EnableSwagger(c =>
{
    c.RootUrl(x => baseUrl);
    c.SingleApiVersion("swagger", title);
}).EnableSwaggerUi();

// This does not work.  JSON file is located at http://{root}/swagger
this.EnableSwagger("{apiVersion}", c =>
{
    c.RootUrl(x => baseUrl);
    c.SingleApiVersion("swagger", title);
}).EnableSwaggerUi();

// This does not work.  JSON file is located at http://{root}/foo/swagger
this.EnableSwagger("foo/{apiVersion}", c =>
{
    c.RootUrl(x => baseUrl);
    c.SingleApiVersion("swagger", title);
}).EnableSwaggerUi();

How can we configure Swashbuckle so the file is named "swagger.json" and it is accessed from a different path from "/swagger/docs" - preferably the root of the application?

like image 778
SonOfPirate Avatar asked Dec 07 '16 16:12

SonOfPirate


People also ask

Where is the Swagger json file located?

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 .

What is swashbuckle Swagger?

Swashbuckle is an open source project for generating Swagger documents for Web APIs that are built with ASP.NET Core. There are three core components: AspNetCore. SwaggerGen - provides the functionality to generate JSON Swagger documents that describe the objects, methods, return types, etc.

What is a Swagger json file?

json file. The Swagger specification of the REST API consists of a file of JSON data called swagger. json. This schema file includes endpoint URLs, descriptions, request parameters and response structures for the entire API.


1 Answers

In case anyone is still looking:

this approach worked for me:

app.UseSwagger() changes:

 app.UseSwagger(c =>
 {
      c.RouteTemplate = "SampleApi/swagger/{documentName}/swagger.json";
 });

app.UseSwaggerUI() changes:

 app.UseSwaggerUI(c =>
 {
      c.SwaggerEndpoint("/SampleApi/swagger/v1/swagger.json", "Sample API");
      c.RoutePrefix = "SampleApi/swagger";
 });
like image 163
Nandun Avatar answered Nov 02 '22 23:11

Nandun