2016-05-12 26 views
2

有沒有方法可以將模型信息(如有效值,默認值,摘要和其他備註)添加到易怒輸出中?將模型信息添加到易怒輸出

例如在c#中,我會如何將以下評論和屬性添加到swagger中?

/// <summary> 
/// A clear summary 
/// </summary> 
/// <remarks> 
/// Some remarks 
/// </remarks> 
public class A 
{ 
    public A() 
    { 
     _Field_A = 0; 
     _Field_B = string.Empty; 
    } 

    private int _Field_A { get; set; } 

    [Range(0, 150)] 
    public int Field_A 
    { 
     get 
     { 
      return _Field_A; 
     } 

     set 
     { 
      if (value != null) { _Field_A = value; } 
     } 
    } 

    private string _Field_B { get; set; } 

    /// <summary> 
    /// Field_B summary 
    /// </summary>  
    public string Field_B 
    { 
     get 
     { 
      return _Field_B; 
     } 

     set 
     { 
      if (value != null) { _Field_B = value; } 
     } 
    } 
} 

回答

4

您將需要確保您的項目屬性的XML文檔文件的創建: 項目屬性>生成>檢查XML文檔文件箱

然後你就可以取消或以下行添加到您的SwaggerConfig。 cs文件: c.IncludeXmlComments(GetXmlCommentsPath());

+2

生成的XML文檔始終保持最新也是非常重要的! http://stackoverflow.com/questions/21895257/how-can-xml-documentation-for-web-api-include-documentation-from-beyond-the-main – chhenning

2

根據Swashbuckle github,您可以啓用XML註釋,這將允許您相應地添加元數據。

httpConfiguration 
    .EnableSwagger(c => 
     { 
      c.SingleApiVersion("v1", "A title for your API"); 
      c.IncludeXmlComments(GetXmlCommentsPathForControllers()); 
      c.IncludeXmlComments(GetXmlCommentsPathForModels()); 
     });