We have asp.net core webapi. We added Microsoft.AspNetCore.Mvc.Versioning
and Swashbuckle
to have swagger UI.
We specified controllers as this:
[ApiVersion("1.0")]
[Route("api/v{version:apiVersion}/[controller]")]
public class ContactController : Controller
{
When we run swagger ui we get version as parameter in routes:
How to set-up default "v1" for route ? If version 2 come to the stage how support swagger ui for both versions ?
@Alezis Nice approach, but if you are using the latest version of Microsoft.AspNetCore.Mvc.Versioning (2.3.0) library,
ControllerAttributes()
andActionAttributes()
are deprecated, you can updateDocInclusionPredicate
as follows:Swashbuckle.AspNetCore github project helps me a lot.
Instead of tweaking the OpenAPI document, you can use the library provided by Microsoft that adds versions to the API Explorer. That way the versions are provided before Swashbuckle (or another toolchain) needs it and allows you to avoid custom code.
Microsoft.AspNetCore.Mvc.Versioning.ApiExplorer
I was able to get versions configured correctly after adding the package and this block of code.
If working with .Net Core 3, Basically I have taken @Alezis's solution and updated it to work with .Net core 3:
@ArlanG it helped me, thanks. It works in Asp.Net Core 3.1. There is one small clarification from my point of view. If you want to get more similar behavior like main answer @Alezis method implementation of DocInclusionPredicate() can be:
In this case when you choose a version on SwaggerUi page, it will show only controller methods that are mapped to this version.
I found that using the method ArlanG highlighted took
{00:00:00.0001905}
to complete whereas runningtook
{00:00:00.0000626}
I know we're talking about minor differences but still.
In Asp.core 2.+ Add this class:
next add below codes in configureServices method in startup:
then add below codes in configure method in startup:
in Asp.core 3.+ add these classes:
next add below codes in ConfigureServices method in startup:
then add below codes in configure method in startup: