Introduction
OpenAPI documentation is essential for modern API development. It helps developers understand endpoints, request/response formats, authentication, and how to interact with your API without digging into the code.
For a long time, Swashbuckle has been the most popular library for generating OpenAPI (Swagger) documentation in ASP.NET Core. However, with newer versions like ASP.NET Core 10, Microsoft provides built-in support for OpenAPI generation without needing Swashbuckle.
In this article, we will learn how to generate OpenAPI documentation in ASP.NET Core 10 without Swashbuckle in a simple and practical way.
What is OpenAPI?
OpenAPI is a standard specification for describing REST APIs.
It defines:
Available endpoints
HTTP methods (GET, POST, PUT, DELETE)
Request parameters
Response formats
Authentication methods
It is usually represented as a JSON or YAML file.
Why Avoid Swashbuckle?
While Swashbuckle is powerful, there are reasons to avoid it:
Additional dependency in your project
Slower startup in some cases
Less control compared to native implementations
ASP.NET Core 10 provides a cleaner and lightweight alternative using built-in APIs.
Built-in OpenAPI Support in ASP.NET Core 10
ASP.NET Core now includes native OpenAPI support via minimal APIs and built-in services.
You can generate OpenAPI documents using:
Microsoft.AspNetCore.OpenApi
n- Endpoint metadata
This approach is lightweight, fast, and fully integrated with the framework.
Step-by-Step Implementation
Step 1: Create a New ASP.NET Core Web API Project
dotnet new webapi -n OpenApiDemo
cd OpenApiDemo
Step 2: Add OpenAPI Package
Install the built-in OpenAPI package:
dotnet add package Microsoft.AspNetCore.OpenApi
Step 3: Configure Services in Program.cs
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddOpenApi();
var app = builder.Build();
This registers OpenAPI services in your application.
Step 4: Enable OpenAPI Endpoint
app.MapOpenApi();
This exposes the OpenAPI document, usually at:
/openapi/v1.json
Step 5: Create Sample Endpoint
app.MapGet("/products/{id}", (int id) =>
{
return new { Id = id, Name = "Sample Product" };
})
.WithName("GetProduct")
.WithOpenApi();
Here:
WithName() gives the endpoint a unique identifier
WithOpenApi() includes it in the OpenAPI document
Step 6: Run the Application
dotnet run
Open your browser and visit:
https://localhost:5001/openapi/v1.json
You will see the generated OpenAPI JSON.
Adding More Metadata to OpenAPI
You can enhance your documentation using metadata.
Example: Add Description and Tags
app.MapPost("/products", (Product product) =>
{
return Results.Created($"/products/{product.Id}", product);
})
.WithName("CreateProduct")
.WithOpenApi(operation =>
{
operation.Summary = "Create a new product";
operation.Description = "Adds a product to the system";
return operation;
});
Join the conversation! Your thoughts help the community grow.