Jump-Start: Swaggerizing ASP.NET Core

As a consumer of an API service, how do you know what functionality the service exposes?

Easy answer: Swagger support

Swagger is a framework that enables you to visually discover an API service. With it, you can inspect…

  • Each service
  • The supported operations (endpoints)
  • The parameters for each service operation
  • The model (data) schema that each operation exposes

You can even invoke any of the service operations to “try it out.”

Here is what the Swagger UI looks like for you, the service consumer…

So, as a service producer, how do you add Swagger support to your ASP.NET Core Application, fast?

Here are the (very) simple jump-start steps:

1. Start with a quick trip out to NuGet and snag the Swashbuckle.AspNetCore package (note the naming here — the preferred open-source package for Swagger support for .net applications is entitled ‘Swashbuckle‘):

2.  Add the following configuration code to both the Configure and ConfigureServices methods of your Startup.cs class:

3. Run your app and append “swagger” to your host name. So, the base service URI, http://localhost:8291 becomes http://localhost:8291/swagger:

Here’s a nice catch that’ll save you an hour of scratching your head: In the Startup.cs configuration code above, make sure that the first argument for the SwaggerDoc and SwaggerEndpoint methods, shown in small blue boxes above, are identical in case. Got the following error message when the case for these two arguments did not match:

Hope this helps!

Rob Vettor is a Senior Developer Consultant with Microsoft, helping Microsoft Enterprise customers construct high-quality software. Rob focuses on Azure PaaS, including the Service Fabric and Azure App Services platforms. A former user group leader, 3-time C# MVP and co-author of the book Entity Framework 6 Recipes, Rob is a frequent presenter at technical conferences. Reach out at robvet@microsoft.com.

Leave a Reply

Your email address will not be published. Required fields are marked *