Recently I start working on open source project using ASP.Net Core 2.0 & ReactJS
you guy can take a look at Awesome CMS-Core

Recent post:

So in order to let Swagger know about our API versioning config we have to modify our code little bit

public void ConfigureServices(IServiceCollection services)
{
   services.AddSwaggerGen(c =>
     {
       c.SwaggerDoc("v1", new Info
        {
          Version = "v1",
          Title = "Awesome CMS Core API V1",
          Contact = new Contact { Name = "Tony Hudson", Email = "", Url = "https://github.com/ngohungphuc" }
        });

      c.SwaggerDoc("v2", new Info
       {
         Version = "v2",
         Title = "Awesome CMS Core API V2",
         Contact = new Contact { Name = "Tony Hudson", Email = "", Url = "https://github.com/ngohungphuc" }
       });

      c.ResolveConflictingActions(apiDescriptions => apiDescriptions.First());
            });
}

public void Configure(IApplicationBuilder app)
{
     app.UseSwagger();
     app.UseSwaggerUI(c =>
     {
       c.SwaggerEndpoint($"/swagger/v1/swagger.json", "Awesome CMS Core API V1");

       c.SwaggerEndpoint($"/swagger/v2/swagger.json", "Awesome CMS Core API V2");
     });
}

In order for swagger to group API into version we need to decorate our controller like

    [ApiVersion("1.0")]
    [ApiExplorerSettings(GroupName = "v1")]
    [Route("api/v{version:apiVersion}/account/[action]")]

When we have multiple version we will have one issue that we have 2 identical api just different version so swagger will throw error

“Not supported by Swagger 2.0: Multiple operations with path ‘some path’ and method ‘some Http request'”

To work around this issue we will add this line in ConfigureServices

c.ResolveConflictingActions(apiDescriptions => apiDescriptions.First());

Then we can run our app and we can choose different API version
chrome_2018-04-26_23-36-19

Happy Coding !!!

Advertisements