如何在 Swashbuckle 中添加 API 描述?

How to add API description in Swashbuckle?

我是 Swashbuckl 或 Swagger 的新手,但我创建了一个 Web API,它需要从我的客户端使用 Swagger 创建文档。我已经创建了,但他们要求 API 版本详细信息显示在 Swageer UI 中,我不太确定如何实现。

这是我的代码:

public class SwaggerConfig
{
    public static void Register()
    {
        var thisAssembly = typeof(SwaggerConfig).Assembly;

        GlobalConfiguration.Configuration
            .EnableSwagger(c =>
                {
                    c.SingleApiVersion("v1", "Demo Api");
                    c.IncludeXmlComments(string.Format(@"{0}\bin\WebApi.XML",
                                         System.AppDomain.CurrentDomain.BaseDirectory));
                })
        .EnableSwaggerUi();

    }
}

控制器示例:

     /// <summary>
    /// Get the Next IdKey
    /// </summary>
    /// <remarks>Get the Next IdKey from Dettagli Table</remarks>
    /// <response code="404">Not found</response>
    /// <response code="500">Internal Server Error</response>
    [HttpGet]
    public int GetNextIdDettagli()
    {
        try
        {
            DettagliRepo repo = new DettagliRepo();
            return repo.GetNextIdDettagli();
        }
        catch (Exception ex)
        {
            throw (ex);
        }
    }

这就是所有控制器操作的完成方式。

这是我的 Swagger 的输出 UI:

这是我的客户的预期输出,由 1,2 和 3 标记:

我可以从屏幕截图中了解到他们希望显示 API 描述、标题和其他信息,但我不知道如何显示它们。请帮助我或建议我如何实现该部分。提前致谢。

更新

我已经从 Following link

中获得了 1 和 2

使用以下代码:

                   .EnableSwagger(c =>
    {
        c.RootUrl(req => GetRootUrlFromAppConfig());

        c.Schemes(new[] { "http", "https" });

        c.SingleApiVersion("v1", "Swashbuckle.Dummy")
            .Description("A sample API for testing and prototyping Swashbuckle features")
            .TermsOfService("Some terms")
            .Contact(cc => cc
                .Name("Some contact")
                .Url("http://tempuri.org/contact")
                .Email("some.contact@tempuri.org"))
            .License(lc => lc
                .Name("Some License")
                .Url("http://tempuri.org/license"));
    });

但我仍然需要第 3 点的帮助。谢谢。

您实际上可以创建文档过滤器并使用文档过滤器更新 swagger 文档中的 Tags 节点。

查看下面的示例文档过滤器:

public class DocumentFilter : IDocumentFilter
{
    /// <summary>
    /// This method is for applying the filter
    /// </summary>
    /// <param name="swaggerDoc">Swagger Document</param>
    /// <param name="schemaRegistry">Schema Registry</param>
    /// <param name="apiExplorer">API Explorer</param>
    public void Apply(SwaggerDocument swaggerDoc, SchemaRegistry schemaRegistry, IApiExplorer apiExplorer)
    {
        var methods = swaggerDoc.paths.Select(i => i.Value);
        List<string> tags = new List<string>();
        foreach (var method in methods)
        {
            if (method.delete != null)
            {
                tags.AddRange(method.delete.tags);
            }

            if (method.get != null)
            {
                tags.AddRange(method.get.tags);
            }

            if (method.put != null)
            {
                tags.AddRange(method.put.tags);
            }

            if (method.post != null)
            {
                tags.AddRange(method.post.tags);
            }

            if (method.patch != null)
            {
                tags.AddRange(method.patch.tags);
            }                
        }

        swaggerDoc.tags = new List<Tag>();
        foreach (var tag in tags)
        {
            swaggerDoc.tags.Add(new Tag() { name = tag, description = "This is a group of methods for " + tag });
        }
    }
}