Introduction

The Microsoft.AspNetCore.OpenApi package offers built-in support for generating OpenAPI documents in ASP.NET Core applications. It provides several key features to enhance API documentation. First, it enables the generation of OpenAPI documents at runtime, allowing developers to access them through a dedicated application endpoint. Additionally, it includes "transformer" APIs, which provide flexibility to modify the generated documentation as needed. Furthermore, the package supports the creation of multiple OpenAPI documents from a single application, making it easier to manage and organize API specifications efficiently.

Agenda

In this article, we will understand the following concepts in detail.

Simple .NET Core Web API Project

We will create a simple .Net core Web API project with the following steps in Visual Studio 2022.

  1. Open Visual Studio 2022, Click on "Create new Project" then select the "ASP.NET Core Web API" option and click on "Next".
    Visual studion
  2. Enter the project named "OpenAPIDocDemo" in the preferred location like below and click on "Next".
    OpenAPI
  3. The Additional Information window is shown below. for this simple demo, we are keeping the info as-is and then clicking on the "Create" button.
    Create
  4. After clicking on the "Create" button it shows the project like below.
    Project
  5. We are done with a simple .Net Core Web API project.

Understanding about OpenAPI documentation

In the created project, we can observe the following referenced package "Microsoft.AspNetCore.OpenApi" provides built-in support of OpenAPI documentation.

OpenAPI documentation'

The Program.cs file has the following highlighted information about OpenAPI support.

OpenAPI support

Let's run the project and append the following "openapi/v1.json" in the browser localhost URL (Ex: localhost:7158/openapi/v1.json). We can observe the following screen.

Localhost

It indicates without doing even a small line of code changes, The.NET9 Web API supports/generates JSON documents with the standard format of API documentation.

Swagger UI Inclusion

Let's make very minimal changes in this demo project to include Swagger UI (our very owned habituated API documentation.

That's it. We are done with Swagger UI inclusion in our .NET API project. Let's run the project again with the sample URL (https://localhost:7158/swagger/index.html). We can see the following page.

.NET API project

ReDoc UI Open API Document

Let's try to include ‘ReDoc” API documentation as well in our project.

It's that simple..!!. We just included ReDoc API documentation in our .NET API project. Let's run the project again with the sample URL (https://localhost:7158/api-docs/index.html). We can see the following page.

ReDoc API

Scalar UI Open API Document

Let's include ‘Scalar UI” Open API documentation in our project as well.

Let's run our project with the sample URL (https://localhost:7158/scalar/), and the screen looks like the one below.

Project

The Scalar API provides more GUI-friendly features with the support of multiple programming languages of sample code if needed. These are really helpful whenever other development teams try to include/call our API in an easy way.

Let's try for a sample C# code for one of our existing Get methods in this sample project in the following way.

Get method

References

The Source Code of this aforesaid project is included in this article as well for reference (OpenAPIDocDemo.zip).

Summary

In this article, we explored Simple .NET Core Web API with details about Open API documentation and other API documentation UI integrations of Swagger UI, ReDco, and Scalar. These are really helpful for our day-to-day projects in APIs for testing or providing the API documentation of other development teams.​​​​​​