Including XML Comments in Swagger under ASP.NET Core

For all of you using Swagger, a great tool or let's better say Framework to Document and Represent your RESTful Web API Service under ASP.NET Core, you will probably be interested in following few lines of Code that enables Swagger to use your Project XML Documentation as information source.

First of all, you have to enable Generation of XML Documentation for your ASP.NET Core Project under Project Properties > Build and Check the "XML Documentation file" checkbox. After that you need to change your Startup.cs file and add following lines to your Configuration for Swagger:

public void ConfigureServices(IServiceCollection services)

{

// Add framework services.

services.AddMvc();

 

// Inject an implementation of ISwaggerProvider with defaulted settings applied

services.AddSwaggerGen();

 

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(basePath + "\\MyProjectName.xml");

});

 

}

 

Hope this will save you some time searching for on the net, and continue to focus on more important things in your Project.


Posted Sep 30 2016, 04:44 PM by Armin Kalajdzija
developers.de is a .Net Community Blog powered by daenet GmbH.