I'm using aspnet-api-versioning in my WebApi project and it works fine. Versions are not hardcoded, they are retrieved from the namespaces.
Now I would like to retrieve the list of already implemented versions. I want to create some endpoint (let's call it GetApiVersions) that user could call to retrieve a simple collection of existing api version numbers, e.g. [1, 2, 3].
In the code responsible for selecting a correct api version based on the retrieved request (CurrentImplementationApiVersionSelector
, SelectVersion
method), there is an ApiVersionModel
parameter with the ImplementedApiVersions
property.
The ImplementedApiVersions
property seems to be exactly what I need, but I have no idea how to access it within my GetApiVersions endpoint. Is there a way to retrieve it? Or is there any other way to programmatically retrieve a list of implemented api versions?
I wasn't able to access ImplementedApiVersions
property. I had to come up with a workaround. I've created a service (called ApiVersionService) with a static field (List<string> ApiVersions
). Service allows to register and read registered versions. As I'm using swagger, I registered all versions in SwaggerConfig.cs
file:
configuration.EnableSwagger(ApiDocConfiguration.RouteTemplate, swagger =>
{
swagger.MultipleApiVersions(
(apiDescription, version) => apiDescription.GetGroupName() == version,
info =>
{
foreach (var group in apiExplorer.ApiDescriptions)
{
apiVersionService.AddApiVersion(group.Name);
// some other code
}
});
});
Simple endpoint allows me to retrieve all versions:
[HttpGet]
[Route("versions")]
public IHttpActionResult GetVersions()
{
// apiVersionService returns the data stored in ApiVersions field
var versions = apiVersionService.GetApiVersions();
return Ok(versions);
}