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

时间:2019-10-29 17:27:46

标签: asp.net-core .net-core asp.net-core-2.0 asp.net-core-webapi swashbuckle

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

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

项目结构
enter image description here

控制器

T

请注意,控制器没有def Delta_Time_One_State(CleanState): Lgt_cut_Time = [] Lgt_Uncut_Time = [] for counter, value in enumerate(CleanState[1:]): DeltaT = value[0] - CleanState[counter][0] calculated_data = [value[0] + DeltaT / 2, DeltaT] if value[1] == 0: Lgt_cut_Time.append(calculated_data) else: Lgt_Uncut_Time.append(calculated_data) 属性,因为我希望版本控制由名称空间定义

Startup.cs

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

问题
它无法生成swagger.json文件。当我浏览网址[ApiVersion]时看到错误 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(); } }

1 个答案:

答案 0 :(得分:2)

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