@Marta what is your goal? What do you expect to do with the API versions once you have them? That will dictate the solution.
If all you want to do is report this information back in requests, this is built-in; you need only turn it on.
options.ReportApiVersions = true;
This will return the api-supported-versions and api-deprecated-versions headers. You can see this and more information in the Version Discovery wiki topic.
You can also use the GetApiVersionModel extension method on the currently executing action in your controller. Again - depending on when and where you are in the pipeline determines how you retrieve the information.
For documentation type scenarios, you want the API Versioning API Explorer package as well. That will provide an IApiExplorer implementation for Web API that will collate all APIs and routes by API version. You can reorder them as desired. This is the same approach/technique that Swagger/OpenAPI generators use to build their documents. You integrate it into your application via:
var apiExplorer = configuration.AddVersionedApiExplorer(options => { });
// TODO: use the API Explorer
Exactly how you use the API Explorer is up to you. Do note that the built-in IApiExplorer interface doesn't support the notion of grouping. If you rely purely on the interface, you'll get a flatten list. This will matter depending on where you reference the API Explorer implementation. If you request the API Explorer later in your application, you either have to work within the confines of the original IApiExplorer interface or cast up to VersionedApiExplorer. The AddVersionedApiExplorer
extension method always returns a VersionedApiExplorer. You can also imperatively create and/or extend the VersionedApiExplorer rather than using the provided extension or configuration.Services.GetApiExplorer()
.
You can see an example of integration here. However, if you really want to see how it's used and you're not familiar with IApiExplorer, then you probably want to look at a repo such as Swashbuckle.
I hope that helps. Feel free to ask more questions.