0

I have .NET Core 2.2 application. I am trying to set up API with different versions using Microsoft.AspnetCore.Mvc.Versioning nugetpackage. I followed the samples provided in the repository.

I want to use an API version based on the name of the defining controller's namespace.

Project Structure
enter image description here

Controllers

namespace NetCoreApiVersioning.V1.Controllers
{
    [ApiController]
    [Route("[controller]")]    
    [Route("v{version:apiVersion}/[controller]")]
    public class HelloWorldController : ControllerBase
    {
        public IActionResult Get()
        {
            return Ok();
        }
    }
}

namespace NetCoreApiVersioning.V2.Controllers
{
    [ApiController]    
    [Route("[controller]")]    
    [Route("v{version:apiVersion}/[controller]")]    
    public class HelloWorldController : ControllerBase
    {
        public IActionResult Get()
        {
            return Ok();
        }
    }
}

Note the controllers does not have [ApiVersion] attribute becuase i want the versioning to be defined by the namespace

Startup.cs

    public class Startup
    {

        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
            services.AddApiVersioning(
                options =>
                {
                    // reporting api versions will return the headers "api-supported-versions" and "api-deprecated-versions"
                    options.ReportApiVersions = true;

                    // automatically applies an api version based on the name of the defining controller's namespace
                    options.Conventions.Add(new VersionByNamespaceConvention());
                });

            services.AddVersionedApiExplorer(
               options =>
               {
                   // add the versioned api explorer, which also adds IApiVersionDescriptionProvider service
                   // note: the specified format code will format the version as "'v'major[.minor][-status]"
                   options.GroupNameFormat = "'v'VVV";

                   // note: this option is only necessary when versioning by url segment. the SubstitutionFormat
                   // can also be used to control the format of the API version in route templates
                   options.SubstituteApiVersionInUrl = true;
               });

            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", new Info { Title = "API v1 ", Version = "v1" });
                c.SwaggerDoc("v2", new Info { Title = "API v2", Version = "v2" });
            });

            // commented code below is from  
            // https://github.com/microsoft/aspnet-api-versioning/tree/master/samples/aspnetcore/SwaggerSample      

            //services.AddTransient<IConfigureOptions<SwaggerGenOptions>, ConfigureSwaggerOptions>();
            //services.AddSwaggerGen(
            //    options =>
            //    {
            //        // add a custom operation filter which sets default values
            //        //options.OperationFilter<SwaggerDefaultValues>();


            //        // integrate xml comments
            //        //options.IncludeXmlComments(XmlCommentsFilePath);
            //    });
        }

        public void Configure(IApplicationBuilder app, IHostingEnvironment env, IApiVersionDescriptionProvider provider)
        {
            // remaining configuration omitted for brevity

            // Enable middleware to serve generated Swagger as a JSON endpoint.
            app.UseSwagger();

            app.UseSwaggerUI(
                options =>
                {
                    // build a swagger endpoint for each discovered API version
                    foreach (var description in provider.ApiVersionDescriptions)
                    {
                        options.SwaggerEndpoint($"/swagger/{description.GroupName}/swagger.json", description.GroupName.ToUpperInvariant());
                    }
                });

            app.UseMvc();
        }        
    }

Issue
It is not able to generate swagger.json file. When i browse url /swaggger i see error undefined /swagger/v1/swagger.json

1 Answer 1

2

found.. i am missing [HttpGet] attribute in ActionMethods

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.