2014-09-02 66 views
2

我在VS2013中有几个类库和一个Web API项目的解决方案。设置Swagger UI时遇到一些问题。首先,当我为Web API项目设置swashbuckle时,我只能指向一个文档XML文件。有没有办法指出包含多个XML文件,以便Swagger不仅可以在控制器中为我的路由选择文档,而且还可以从我的其他项目获取域对象?下面是从SwaggerConfig.csWeb Api中的端点和类文档中的Swagger UI设置

SwaggerSpecConfig.Customize 
    (
     c => 
     { 
      c.IncludeXmlComments(Path.Combine(dirPath, projName + ".xml")); 
     } 
    ); 

如果我添加多个XML文件我的代码,它只是拿起从IncludeXmlComments的最后一个文件。

其次,我在JSON返回时使用骆驼情况下我的DTO

formats.JsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver(); 

但是当我看看扬鞭UI响应模型和模型模式的响应类,我看到确切的类属性名而不是在端点被击中时返回的JSON模式。有没有办法在Swagger UI文档页面中显示确切的JSON模式?

回答

0

添加多个XML文件配置这样的swagger。

 SwaggerSpecConfig.Customize(c => 
     { 
      // single XML Comment Files 
      //c.IncludeXmlComments(GetXmlCommentsPath()); 

      // Multiple XML Comment Files 
      string[] paths = GetXmlCommentsPaths(); 
      foreach (string xmlCommentsPath in paths) 
      { 
       c.OperationFilter(new ApplyActionXmlComments(xmlCommentsPath)) 
        .ModelFilter(new ApplyTypeXmlComments(xmlCommentsPath)); 
      } 
     }); 
+0

我一直与代码修修补补,我不知道我是如何得到它的工作。当我使用上面的代码时,它可以工作,但是当我使用你的代码时,我只能得到部分文档。我会尝试恢复我的代码并使用您的解决方案。仅供参考,在ApplyActionXmlComments和ApplyTypeXmlComments中,构造函数需要一个XPathDocument。 – Kenpachii 2014-10-01 14:17:48

+0

它正在为我工​​作。我不知道你使用的是什么版本,但我使用的是4.1.0-rc2预发行版,它以文件的路径作为字符串。 – Hamza 2014-10-02 07:06:31

0

4.1版本后,你可以这样写:

c.IncludeXmlComments("File1_Path"); 
c.IncludeXmlComments("File2_Path")); 

又见here

0

我使用的5.6.0和多个XML文档为我工作:

var dir = new DirectoryInfo(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "bin")); 
foreach (var fi in dir.EnumerateFiles("*.xml")) 
{ 
    c.IncludeXmlComments(fi.FullName); 
} 
相关问题