Step 1. Create an ASP.NET Core Web Application and select API template and DemoSwaggerUI as your project name
Step 2. Install Swashbuckle from Package Manager in your project
Install-Package Swashbuckle.AspNetCore -Version 5.0.0
Step 3. Insert the following statements to Startup.cs
using Microsoft.OpenApi.Models;public void ConfigureServices(IServiceCollection services){ services.AddControllers(); // Register the Swagger generator, defining 1 or more Swagger documents services.AddSwaggerGen(c => { c.SwaggerDoc("v1", new OpenApiInfo { Version = "v1", Title = "ToDo API", Description = "A simple example ASP.NET Core Web API", TermsOfService = new Uri("https://example.com/terms"), Contact = new OpenApiContact { Name = "Nicky Liu", Email = "nicky@zedotech.com", Url = new Uri("https://www.zedotech.com"), }, License = new OpenApiLicense { Name = "Use under LICX", Url = new Uri("https://example.com/license"), } }); });}public void Configure(IApplicationBuilder app){ // Enable middleware to serve generated Swagger as a JSON endpoint. app.UseSwagger(); // Enable middleware to serve swagger-ui (HTML, JS, CSS, etc.), // specifying the Swagger JSON endpoint. app.UseSwaggerUI(c => { c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1"); }); app.UseRouting(); app.UseEndpoints(endpoints => { endpoints.MapControllers(); });}
Then run the application, you will see this screen in localhost:xxxxx/index.html
Step 4. Display API comments in UI
First, you need to insert the following statements in DemoSwaggerUI.csproj
<PropertyGroup><TargetFramework>netcoreapp3.1</TargetFramework><GenerateDocumentationFile>true</GenerateDocumentationFile><NoWarn>$(NoWarn);1591</NoWarn></PropertyGroup>
Second, add a triple-slash comment to WeatherForecast/Get action
/// <summary>/// Get weathers/// </summary>/// <returns></returns>
Then re-run the application, you will see your description like this
Step 5. Display more description details
Insert comment to <remarks> block
/// <summary>/// Get weathers/// </summary>/// <remarks>/// Description////// GET /WeatherForecast/// This will return a WeatherForecast object array////// </remarks>/// <returns></returns>
Then re-run the application, you will see your description like this