Asp.net-Core

獲取 ASP Core 的 XML 註釋輸出文件位置

  • June 18, 2016

我已將Swashbuckle包添加到我的 ASP Core 項目中。

我想將 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));

我看到的不好的解決方案:

  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 文件與應用程序同名。我目前在我的項目中使用此程式碼,它工作正常。

引用自:https://stackoverflow.com/questions/36691004