Is it possible to create multiple Swagger document URLs?

Viewed 8578

I have a website which is using Web API 2 and I am using Swashbuckle Swagger to generate API documentation and export the JSON configuration for Azure API Management. In my scenario it would be ideal if the APIs could be grouped by a value defined in the Swagger document URL (http://example.com/swagger/{my-api-group}). However, after some research I could not find a way to implement this.

From what I can understand, at the moment in order for me to change the Swagger export file, I need to restart the environment each time there is a change and this is not ideal in my scenario.

I have tried to see if I can get the open source code from the NuGet package to see if I could understand the inner workings of Swashbuckle. Furthermore, I am aware that this can be achieved using "Swashbuckle.AspNetCore", but this is not an option since I am integrating this feature in a big .NET Framework solution which would be a staggering task to accomplish.

1 Answers

Yes you can do that, here is an example:
http://swagger-net-test-multiapiversions.azurewebsites.net/swagger/ui/index

And the source code for that is here:
https://github.com/heldersepu/Swagger-Net-Test/tree/MultiApiVersions

The key to that is this piece on the configuration:

// If your API has multiple versions, use "MultipleApiVersions" instead of "SingleApiVersion".
// In this case, you must provide a lambda that tells Swagger-Net which actions should be
// included in the docs for a given API version. Like "SingleApiVersion", each call to "Version"
// returns an "Info" builder so you can provide additional metadata per API version.
//
c.MultipleApiVersions(
    (apiDesc, targetApiVersion) => targetApiVersion.Equals("default", StringComparison.InvariantCultureIgnoreCase) ||     
                                   apiDesc.Route.RouteTemplate.StartsWith(targetApiVersion, StringComparison.InvariantCultureIgnoreCase), 
                    (vc) =>
                    {
                        vc.Version("default", "Swagger_Test");
                        vc.Version("v1_0", "Swagger_Test V1_0");
                        vc.Version("v2_0", "Swagger_Test V2_0");
                    });

Another option could be leveraging the filter on the latest UI:

Related