Hopefully someone tried something similar with the versioned API in MVC 6 and Swagger to display documentation on different versions.
I am using the recommended version control API in MVC 6 on this ASP.NET 5 repository . The only change I made was the GetVersion method to read the api version from the custom HTTP request header:
//in VersionRangeValidator.cs public static string GetVersion(HttpRequest request) { //return request.Query["version"]; if (!string.IsNullOrWhiteSpace(request.Headers[Constants.CommonRoutingDefinitions.ApiVersionSegmentName])) { return request.Headers[Constants.CommonRoutingDefinitions.ApiVersionSegmentName]; } return Constants.CommonRoutingDefinitions.CurrentApiVersion; }
and I have a controller like this:
[Route("api/[controller]")] [Produces(Constants.MediaTypeNames.ApplicationJson)] public class TagsController : Controller { private readonly ITagService _tagService; public TagsController(ITagService tagService) { _tagService = tagService; }
Versioning happens with a custom http header to
Get / api / tags
Content-Type: application / json
will result in GetTags () by default, since no header is specified, but
Get / api / tags
api version: 2
Content-Type: application / json
will hit the GetTagsV2 () action.
I added the Swagger UI and Swagger GEN libraries the following steps this blog , so in my project.json I have the following dependencies:
"Swashbuckle.SwaggerGen": "6.0.0-rc1-final", "Swashbuckle.SwaggerUi": "6.0.0-rc1-final"
Then in my Startup.cs I add Swagger to the pipeline with
and I configure Swagger as follows:
private void ConfigureSwagger(IServiceCollection services) { services.AddSwaggerGen(); services.ConfigureSwaggerDocument(options => { options.MultipleApiVersions(new Swashbuckle.SwaggerGen.Info[] { new Swashbuckle.SwaggerGen.Info { Version = "v1", Title = "MyApp API", Description = "A RESTful API" }, new Swashbuckle.SwaggerGen.Info { Version = "v2", Title = "MyApp API (v2)", Description = "A RESTful API" } }, (description, version) => { //description is an instance of ApiDescription and //version is either "v1" or "v2" //depending on the user choice in swagger UI page //TODO, how can I know whether the action belongs to v1 or to v2 to return true or false as appropriate? }); options.OperationFilter(new Swashbuckle.SwaggerGen.XmlComments.ApplyXmlActionComments(Configuration["Documentation:SwaggerDocXml"])); }); services.ConfigureSwaggerSchema(options => { options.DescribeAllEnumsAsStrings = true; options.ModelFilter(new Swashbuckle.SwaggerGen.XmlComments.ApplyXmlTypeComments(Configuration["Documentation:SwaggerDocXml"])); }); }
The problem is that I don’t know how to get the description (which is an instance of Microsoft.AspNet.Mvc.ApiExplorer.ApiDescription) the necessary information to find out whether this action should be displayed in the Swagger user interface or does not depend on the specified version . Any advice would be greatly appreciated. This would help to understand how this ASP.NET 5 repository works for version control, because I still do not understand it and cannot find a good explanation of how action restrictions work.
PS: https : //stackoverflow.com/a/166268/2128/3168/en/css/simple/contentions/357480/... helped me implement version control using MVC 6, but I couldn’t learn much about how Swagger would integrate with this API version control method.