Skip to content

Instantly share code, notes, and snippets.

@farhad-taran
Created September 6, 2020 19:49
Show Gist options
  • Save farhad-taran/f2ccf092fcad3a1d34af761a308cdb4b to your computer and use it in GitHub Desktop.
Save farhad-taran/f2ccf092fcad3a1d34af761a308cdb4b to your computer and use it in GitHub Desktop.
Swagger and Swashbuckle XML comments

To enable XML documentation such as comments on controllers to appear in swagger follow the following steps.

Enable XML document generation for the Assembly:

<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
    <DocumentationFile>bin\Debug\netcoreapp3.1\PHDV.Deliveroo.Webhooks.Api.xml</DocumentationFile>
</PropertyGroup>

<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
    <DocumentationFile>bin\Release\netcoreapp3.1\PHDV.Deliveroo.Webhooks.Api.xml</DocumentationFile>
</PropertyGroup>

Add the path to the XML file to Swagger options in the Startup.cs:

services.AddSwaggerGen(options =>
        {
            var xmlFileName = $"{GetType().Assembly.GetName().Name}.xml";
            var xmlFilePath = Path.Combine(AppContext.BaseDirectory, xmlFileName);
            options.IncludeXmlComments(xmlFilePath);
            options.SwaggerDoc("v1", new Microsoft.OpenApi.Models.OpenApiInfo
            {
                Title = _serviceName,
                Version = "v1",
            });
        });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment