使用aspnet-api-versioning时无法创建swagger.json文件

问题描述 投票:0回答:1

我有.NET Core 2.2应用程序。我正在尝试使用Microsoft.AspnetCore.Mvc.Versioning nugetpackage设置具有不同版本的API。我遵循了存储库中提供的samples

我想使用基于定义控制器名称空间名称的API版本。

项目结构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();
        }
    }
}

注意,控制器没有[ApiVersion]属性,因为我希望版本控制由名称空间定义

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();
        }        
    }

问题它无法生成swagger.json文件。当我浏览网址/swaggger时,我看到错误undefined /swagger/v1/swagger.json

asp.net-core .net-core asp.net-core-2.0 asp.net-core-webapi swashbuckle
1个回答
0
投票

找到..我在ActionMethods中缺少[HttpGet]属性

© www.soinside.com 2019 - 2024. All rights reserved.