8

I've added Swashbucklepackage to my ASP Core project.

I'd like to configure Swagger to use auto-generated by VS xml comments.

The problem is that I can't find the way to get that location:

  1. PlatformServices.Default.Application.ApplicationBasePath - points to the project root path
  2. Directory.GetCurrentDirectory() - the same
  3. Path.GetFullPath(".") - the same
  4. IHostingEnvironment.WebRootPath - the same

Output folder configured in <project>.xproj by BaseIntermediateOutputPath option.

But I can't get this location in runtime.

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

Bad solutions I see:

  1. add configuration option to AppSettings.json
  2. Relative path from project path (as I'm configuring bin output path).

But I'd like to use this with Docker, CI, localhost, so I don't think this would be the best solution to use hard-coded solution..

dr11
  • 5,166
  • 11
  • 35
  • 77

1 Answers1

2

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

 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 app. I am currently using this code in my project and it works fine.

Naveen B
  • 66
  • 5
  • 1
    I haven't tested it in docker container, but it does work in Azure, self hosted & IIS (localhost). Ideally it should work in docker too since PlatformServices is supposed to expose Platform specific info independent of the Platform. If you do test, please let us know if it works. – Naveen B Jun 21 '16 at 05:28
  • Good stuff. Didn't know `PlatformServices` exists. – Andrei Sep 08 '16 at 13:55
  • Just to point out that this will only work if the application does not have a fullstop in the name since the ChangeExtension will then pick up the last part as the extension... – Sarel Esterhuizen Jan 01 '17 at 16:22