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

dee*_*zen 8 swagger asp.net-core-mvc swashbuckle asp.net-core asp.net-core-1.0

我已经将Swashbuckle包添加到我的ASP核心项目中.

我想将Swagger配置为使用VS xml注释自动生成.

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

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

输出文件夹<project>.xprojBaseIntermediateOutputPath选项配置.

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

var pathToDoc = "????";
options.OperationFilter(new Swashbuckle.SwaggerGen.XmlComments.ApplyXmlActionComments(pathToDoc));
Run Code Online (Sandbox Code Playgroud)

我看到的解决方案不好

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

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

小智 2

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

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

xml 文件与应用程序同名。我目前在我的项目中使用此代码并且运行良好。