【问题标题】:How to add API description in Swashbuckle?如何在 Swashbuckle 中添加 API 描述?
【发布时间】:2016-11-09 07:37:18
【问题描述】:

我是 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 点的帮助。谢谢。

【问题讨论】:

    标签: c# asp.net-mvc api swagger swashbuckle


    【解决方案1】:

    您实际上可以创建一个文档过滤器并使用文档过滤器更新 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 });
            }
        }
    }
    

    【讨论】:

    • 非常感谢,这对我帮助很大。
    猜你喜欢
    • 2019-09-18
    • 1970-01-01
    • 2020-05-26
    • 1970-01-01
    • 1970-01-01
    • 2016-04-14
    • 2020-02-21
    • 2021-07-11
    • 2021-12-31
    相关资源
    最近更新 更多