2016-11-09 101 views
3

我是Swashbuckl或Swagger中的新成员,但我创建了一个Web API,它需要使用来自我的客户端的Swagger创建文档。我已经创建了,但他们需要将API版本的详细信息显示到Swageer UI中,我不太确定如何实现。如何在Swashbuckle中添加API描述?

这里是我的代码:

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

这是怎么回事控制器行动已经取得进展。

这是我扬鞭UI的输出:

enter image description here

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

enter image description here

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

更新

我用下面的代码实现1和2从Following link

    .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("[email protected]")) 
      .License(lc => lc 
       .Name("Some License") 
       .Url("http://tempuri.org/license")); 
    }); 

但我仍需要帮助的点3.感谢。

回答

4

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

谢谢你这么多这对我帮助很大。 – barsan