Is it possible to leverage MultipleApiVersions in Swagger UI / Swashbuckle when using attribute routing?
Specifically, I implemented versioning by:
.EnableSwagger(c => c.MultipleApiVersions(
(apiDesc, version) =>
{
var path = apiDesc.RelativePath.Split('/');
var pathVersion = path[1];
return CultureInfo.InvariantCulture.CompareInfo.IndexOf(pathVersion, version, CompareOptions.IgnoreCase) >= 0;
},
vc =>
{
vc.Version("v2", "Swashbuckle Dummy API V2"); //add this line when v2 is released
// ReSharper disable once ConvertToLambdaExpression
vc.Version("v1", "Swashbuckle Dummy API V1");
}
))
Swagger supports multiple versions. Configure the URL such that Swagger can specify the version correctly.
httpConfiguration
.EnableSwagger(c =>
{
c.MultipleApiVersions(
(apiDesc, targetApiVersion) => ResolveVersionSupportByRouteConstraint(apiDesc, targetApiVersion),
(vc) =>
{
vc.Version("v2", "Swashbuckle Dummy API V2");
vc.Version("v1", "Swashbuckle Dummy API V1");
});
});
.EnableSwaggerUi(c =>
{
c.EnableDiscoveryUrlSelector();
});
private static bool ResolveVersionSupportByRouteConstraint(ApiDescription apiDesc, string targetApiVersion)
{
return apiDesc.ActionDescriptor.ControllerDescriptor.ControllerType.FullName.Contains($"{targetApiVersion}.");
}
References: