为 ASP Core 获取 XML 注释输出文件位置

Get XML comments output file location for ASP Core

我已将 Swashbuckle 程序包添加到我的 ASP 核心项目中。

我想将 Swagger 配置为使用 VS xml 评论自动生成。

问题是我找不到获取该位置的方法:

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

通过 BaseIntermediateOutputPath 选项在 <project>.xproj 中配置的输出文件夹。

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

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

我看到的错误解决方案:

  1. 将配置选项添加到 AppSettings.json
  2. 项目路径的相对路径(因为我正在配置 bin 输出路径)。

但我想将它与 Docker、CI、localhost 一起使用,所以我认为这不是使用硬编码解决方案的最佳解决方案..

您可以尝试以下函数来获取XML文件路径

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

xml 文件与应用同名。我目前在我的项目中使用此代码并且工作正常。