生成“ Aspnet-api-versioning”时,Swagger失败

时间:2018-08-07 02:50:30

标签: c# swagger api-versioning

我正在尝试使用Microsoft的aspnet-api-versioning使Swagger文档适用于我的ASP.NET Core应用程序,并且正在遵循here的指示,因为我不得不将其更改为让它们编译。

但是,每当我到达/ swagger终点时,都会出现以下错误“计数不渲染e,请参见控制台”:

enter image description here

我已经配置了以下内容:

public void ConfigureServices(IServiceCollection aServices)
{
    aServices.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
    aServices.AddMvcCore().AddVersionedApiExplorer(o => o.GroupNameFormat = "'v'VVV");
    aServices.AddApiVersioning();
    aServices.AddSwaggerGen(
        aOptions =>
        {
            IApiVersionDescriptionProvider provider = aServices.BuildServiceProvider()
                                                               .GetRequiredService<IApiVersionDescriptionProvider>();

            foreach (ApiVersionDescription description in provider.ApiVersionDescriptions)
            {
                aOptions.SwaggerDoc(
                    description.GroupName,
                    new Info
                    {
                        Title = $"Sample API {description.ApiVersion}",
                        Version = description.ApiVersion.ToString()
                    });
            }
        }
    );
}

public void Configure(IApplicationBuilder aApp, IHostingEnvironment aEnv)
{
    if (aEnv.IsDevelopment())
        aApp.UseDeveloperExceptionPage();
    else
        aApp.UseHsts();

    aApp.UseHttpsRedirection();
    aApp.UseMvc();
    aApp.UseSwagger();
    aApp.UseSwaggerUI();

    aApp.UseSwaggerUI(
        aOptions =>
        {
            IApiVersionDescriptionProvider provider = aApp.ApplicationServices.GetRequiredService<IApiVersionDescriptionProvider>();

            foreach (ApiVersionDescription description in provider.ApiVersionDescriptions)
            {
                aOptions.SwaggerEndpoint(
                    $"/swagger/{description.GroupName}/swagger.json",
                    description.GroupName.ToUpperInvariant());
            }
        }
    );
}

然后是一个非常简单的控制器:

[Route("v{version:apiVersion}/api/[controller]")]
[ApiController]
public class ValuesController : ControllerBase
{
    // DELETE api/values/5
    [HttpDelete("{id}")]
    public void Delete(int id)
    {
    }

    // GET api/values
    [HttpGet]
    public ActionResult<IEnumerable<string>> Get()
    {
        return new[] {"value1", "value2"};
    }

    // GET api/values/5
    [HttpGet("{id}")]
    public ActionResult<string> Get(int id)
    {
        return "value";
    }

    // POST api/values
    [HttpPost]
    public void Post([FromBody] string value)
    {
    }

    // PUT api/values/5
    [HttpPut("{id}")]
    public void Put(int id, [FromBody] string value)
    {
    }
}

因此,似乎错误缺少Url属性。但是,我期望它来自ASP.NET Api版本控制吗?我在这里错过了什么,我需要做些什么才能使这项工作完成?

0 个答案:

没有答案