我正在构建一个REST服务,它将承载多个控制器(微服务)。作为一个整体,让我们称之为“鲍勃”服务。因此,swagger会显示" Bob“/”Bob Microservices的集合“。然后列出控制器名称。现在,它只显示了XYZ,ABC等。有没有办法让"XYZ -一个XYZ的集合“或者诸如此类的东西摆出来呢?
看起来像swagger在方法上显示/汇总,而不是在控制器上显示。
发布于 2016-11-02 01:30:41
有没有办法让大家大摇大摆地展示"XYZ - XYZ的集合“
是。这是最简单的方法之一。Swagger的ASP.NET核心版本利用了ApiExplorerSettings
属性。您可以设置GroupName
。
public class BobController
{
[ApiExplorerSettings(GroupName="XYZ - A collection of XYZ APIs")]
public IActionResult MyAction()
{
...
}
}
组名称出现在Swagger中,其中组的操作列在下面的操作中。
编辑:是一个基于大锤评论的想法。
Swagger ASP.NET Core使用一个IApiDescriptionGroupCollectionProvider
来构建它的描述组。我们可以实现自己的,使用默认的ApiDescriptionGroupCollectionProvider
作为灵感,并在Startup.ConfigureServices
期间注册提供者。我们的实现将使ApiDescriptionGroups()
方法返回与每个操作的控制器相关联的GroupName
。然后,我们可以将ApiExplorerSettings
属性放在每个控制器上,而不是放在每个操作上。
发布于 2019-05-31 13:14:48
如果您使用的是SwashBackle4.0.x和ASP.NET Core2.x,您可能有类似的东西,它也可以通过包含用于ASP.NET的NuGet包来工作。
using System.Collections.Generic;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Swashbuckle.AspNetCore.Annotations;
namespace MyExample.Controllers
{
/// <summary>
/// Example of a .NET Core controller based on the controller name
/// api/[controller] on ValuesController becomes api/values
/// endpoint: "/Values" from [controller] and name of controller , which is "ValuesController"
/// </summary>
[Route("[controller]")]
[ApiController]
[SwaggerTag("This is an example controller generated by ASP.NET Core 2.x")]
public class ValuesController : ControllerBase
{
...
}
然后,我在Startup.cs方法中的ConfigureServices昂首阔步代码如下所示(编辑后包括来自Iain的贡献,以包含控制器头注释):
services.AddSwaggerGen(c =>
{
// Set Title and version
c.SwaggerDoc("v1", new Info { Title = "My Example API", Version = "v1", Description = "The API for my application" });
// Set the comments path for the Swagger JSON and UI.
var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
// pick comments from classes, including controller summary comments
c.IncludeXmlComments(xmlPath, includeControllerXmlComments: true);
// _OR_ enable the annotations on Controller classes [SwaggerTag], if no class comments present
c.EnableAnnotations();
});
然后我的控制器就会被装饰
发布于 2019-11-14 01:23:27
我正在寻找类似的答案,并希望能够使用控制器类上的摘要XML注释来提供控制器描述。结果表明,您可以通过在启动时的Swagger配置中添加includeControllerXmlComments: true来做到这一点:
services.AddSwaggerGen(c =>
{
// Set the comments path for the Swagger JSON and UI.
var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
c.IncludeXmlComments(xmlPath, includeControllerXmlComments: true);
});
因此:
/// <summary>
/// My controller description
/// </summary>
[Route("api/[controller]")]
[ApiController]
显示为:
https://stackoverflow.com/questions/40369914
复制相似问题