簡體   English   中英

如何在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);
        }
    }

這就是所有Controller操作的完成方式。

這是我的Swagger UI的輸出:

在此輸入圖像描述

這是我的客戶端標記為1,2和3的預期輸出:

在此輸入圖像描述

我可以從屏幕截圖中了解到他們想要顯示API描述,標題和其他信息,但我不知道如何顯示它們。 請幫助我或建議我如何實現這一部分。 提前致謝。

更新

我從以下鏈接中獲得了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文檔中的標簽節點。

請參閱下面的示例文檔過濾器:

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 });
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM