Skip to content

Instantly share code, notes, and snippets.

@sitsh
Last active June 7, 2018 03:53
Show Gist options
  • Save sitsh/c3f27a0765b2ac74a95535bb46ebd201 to your computer and use it in GitHub Desktop.
Save sitsh/c3f27a0765b2ac74a95535bb46ebd201 to your computer and use it in GitHub Desktop.

笔记记录下 swagger 配置过程 quote from

We migrated a project to ASP.NET Core 2 (preview) and needed to configure swagger.

In ASP.NET Core v1 we used this code to load the auto generated xml file into swagger:

services.ConfigureSwaggerGen(options =>

{

//Determine base path for the application.

var basePath = PlatformServices.Default.Application.ApplicationBasePath;

//Set the comments path for the swagger json and ui.

options.IncludeXmlComments(System.IO.Path.Combine(basePath, "ProjectName.xml"));

});

Since PlatformServices is obsolete (https://github.com/aspnet/PlatformAbstractions), you shouldn't use this for your (new) projects anymore.
Like the github page states, you should use the equivalent .NET API instead.

Here is what I use instead of PlatFormAbstractions (PlatformServices) for loading the xml into swagger:

services.ConfigureSwaggerGen(options =>

{

//Determine base path for the application.

var basePath =  AppContext.BaseDirectory;

var assemblyName = System.Reflection.Assembly.GetEntryAssembly().GetName().Name;

var fileName = System.IO.Path.GetFileName(assemblyName +  ".xml");

//Set the comments path for the swagger json and ui.

options.IncludeXmlComments(System.IO.Path.Combine(basePath, fileName));

});

Hint: You could add a check here, if the file really exists before loading it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment