我正在尝试使用 Microsoft 的aspnet-api-versioning让 Swagger 文档适用于我的 ASP.NET Core 应用程序,并按照此处的说明进行操作,这些说明似乎已过时,因为我必须进行更改以使其编译。
但是,每当我到达 /swagger 端点时,我都会收到以下错误“Count not render e, see console”:
我已经配置如下:
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 版本控制?我在这里错过了什么,我需要做些什么来完成这项工作?