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 ?
When updating to .net core 3 I got the following error:
'Unable to cast object of type 'System.Collections.Generic.Dictionary`2[System.String,Microsoft.OpenApi.Models.OpenApiPathItem]' to type 'Microsoft.OpenApi.Models.OpenApiPaths'.'
Fixed this by changes to code to:
At the moment Swashbuckle and Microsoft.AspNetCore.Mvc.Versioning are friends. It works good. I just created test project in VS2017 and checked how it works.
First include these two nuget packages:
Configure everything in
Startup.cs
(read my comments):There two classes that make the trick:
The
RemoveVersionFromParameter
removes from swagger UI this textbox:The
ReplaceVersionWithExactValueInPath
change this:to this:
Controller class looks now as follows:
Code: https://gist.github.com/Alezis/bab8b559d0d8800c994d065db03ab53e