7

我已将Swashbuckle包添加到我的ASP Core项目中。获取ASP核心的XML注释输出文件位置

我想配置Swagger使用VS xml注释自动生成。

的问题是,我无法找到获得该位置的方式:

  1. PlatformServices.Default.Application.ApplicationBasePath - 点到项目的根路径
  2. Directory.GetCurrentDirectory() - 同
  3. Path.GetFullPath(".") - 同样
  4. IHostingEnvironment.WebRootPath - 相同

输出文件夹配置d在<project>.xproj之前BaseIntermediateOutputPath选项。

但我无法在运行时获得此位置。

var pathToDoc = "????"; 
options.OperationFilter(new Swashbuckle.SwaggerGen.XmlComments.ApplyXmlActionComments(pathToDoc)); 

坏的解决方案我看到:

  1. 附加配置选项AppSettings.json
  2. 从项目路径
  3. 相对路径(如我配置斌输出路径)。

但我想与码头工人,CI,本地主机使用,所以我不认为这是使用硬编码解决方案的最佳解决方案..

回答

2

你可以尝试以下方法函数来获取XML文件路径

private string GetXmlCommentsPath() 
     { 
      var app = PlatformServices.Default.Application; 
      return System.IO.Path.Combine(app.ApplicationBasePath, System.IO.Path.ChangeExtension(app.ApplicationName, "xml")); 
     } 

xml文件与应用程序具有相同的名称。我目前在我的项目中使用此代码,它工作正常。

+0

在Docker容器中做这个工作吗? – deeptowncitizen

+0

我没有在Docker容器中测试过它,但它在Azure,自托管和IIS(本地主机)中工作。理想情况下,它也应该在Docker中工作,因为PlatformServices应该公开独立于Platform的Platform特定信息。如果您进行测试,请告诉我们它是否有效。 –

+0

好东西。不知道有'PlatformServices'存在。 – Andrei