Get location of output XML comment files for ASP Core

I added the Swashbuckle package to my ASP Core project.

I would like to configure Swagger to use the automatically generated VS xml comments.

The problem is that I cannot find a way to get this location:

  • PlatformServices.Default.Application.ApplicationBasePath - points to the root path of the project
  • Directory.GetCurrentDirectory() - same
  • Path.GetFullPath(".") - same
  • IHostingEnvironment.WebRootPath - same

The output folder is configured in the <project>.xproj on BaseIntermediateOutputPath .

But I cannot get this location at runtime.

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

Bad solutions that I see:

  • add configuration parameter to AppSettings.json
  • Relative path from the project path (as I am setting up the bin output path).

But I would like to use this with Docker, CI, localhost, so I don’t think that this would be the best solution for using a hard-coded solution.

+7
asp.net-core asp.net-core-mvc swagger swashbuckle
source share
1 answer

You can try the following function to get the path to the XML file

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

The xml file has the same name as the application. I am currently using this code in my project and it works great.

+2
source share

All Articles