Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Leverage MultipleApiVersions in Swagger with attribute versioning

Is it possible to leverage MultipleApiVersions in Swagger UI / Swashbuckle when using attribute routing?

Specifically, I implemented versioning by:

using System.Web.Http;

namespace RESTServices.Controllers.v1
{
    [Route("api/v1/Test")]
    public class TestV1Controller : ApiController
    { ... }

Version 2 would be in a v2 namespace. In a controller named TestV2Controller. The route would have v2 in it.

Is it possible to pass a lambda in that will allow this? I found a sample lambda online which compiled, but then Swagger stopped working completely. Couldn't hit breakpoints or see Swagger in the browser.

like image 539
Hoppe Avatar asked Jun 11 '15 18:06

Hoppe


1 Answers

.EnableSwagger(c => c.MultipleApiVersions(
        (apiDesc, version) =>
        {
            var path = apiDesc.RelativePath.Split('/');
            var pathVersion = path[1];

            return CultureInfo.InvariantCulture.CompareInfo.IndexOf(pathVersion, version, CompareOptions.IgnoreCase) >= 0;
        },
        vc =>
        {
            vc.Version("v2", "Swashbuckle Dummy API V2"); //add this line when v2 is released

            // ReSharper disable once ConvertToLambdaExpression
            vc.Version("v1", "Swashbuckle Dummy API V1");
        }
        ))
like image 191
Hoppe Avatar answered Oct 27 '22 05:10

Hoppe