Rob Sedgwick
Rob Sedgwick

Reputation: 4514

Multiple versions of one controller in Swashbuckle

I am using WebApi and Swagger/Swashbuckle.

I have one version of all my controllers but for one of the controllers, I want to introduce versioning but keep the old version as well.

So for ControllerA where there is just one version I want to carry on doing this.

http://mysite/api/ControllerA

For ControllerB where there are just two versions I want

http://mysite/api/v1/ControllerB http://mysite/api/v2/ControllerB

I can always redirect the unversioned url to v1 if that is possible.

http://mysite/api/ControllerB

I think I fundamentally need to use a SingleApiVersion because most of my controllers only have one version. Inside SwaggerConfig I have the line:

c.SingleApiVersion("v1", "MySite.Api");

However, when I try this I cannot see MyController in the Swagger UI or call it in Postman

[Route("api/v{version:apiVersion}/[controller]")]  
public class MyController : ApiController
{
    [ApiVersion("1.0")]
    [HttpPost]
    public HttpResponseMessage MyMethod([FromBody] MyRequest myRequest)
    {
        return MyCode(myRequest);
    }

    [ApiVersion("2.0")]
    [HttpPost]
    public HttpResponseMessage MyMethod([FromBody] MyRequest myRequest)
    {
        return MyNewCode(myRequest);
    }
}

I think I am probably mixing up the Single and Multiple attributes but I don't want to use multiple versions of the whole API but I only have one with just new versions of one controller. Can this be done?

Upvotes: 2

Views: 2458

Answers (1)

Dominik Oswald
Dominik Oswald

Reputation: 375

Have you tried this?

[ApiVersion( "2.0" )]
[ApiVersion( "3.0" )]
[Route( "api/v{version:apiVersion}/helloworld" )]
public class HelloWorldController : ApiController
{
    public string Get() => "Hello world v2!";

    [MapToApiVersion( "3.0" )]
    public string GetV3() => "Hello world v3!";
}

https://github.com/microsoft/aspnet-api-versioning/wiki/Versioning-via-the-URL-Path

Upvotes: 5

Related Questions