SonOfPirate
SonOfPirate

Reputation: 5494

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?

Upvotes: 10

Views: 5952

Answers (1)

Nandun
Nandun

Reputation: 2042

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

Upvotes: 17

Related Questions