How to set default version in swaggerui using asp net core web api

Setting Default Version in SwaggerUI using ASP.NET Core Web API

SwaggerUI is a popular tool used for documenting and testing APIs. It provides a user-friendly interface to interact with the API endpoints and view the available operations. In ASP.NET Core Web API, SwaggerUI can be integrated to enhance the API and testing experience.

One common requirement while using SwaggerUI is to set a default version for the API. This ensures that when the SwaggerUI is accessed, it automatically displays the documentation for the desired API version. In this article, we will explore how to set the default version in SwaggerUI using ASP.NET Core Web API.

Step 1: Install Required Packages

To get started, we need to install the necessary NuGet packages for SwaggerUI integration in ASP.NET Core Web API. Open the Package Manager and run the following command:

Install-Package .AspNetCore

This will install the Swashbuckle.AspNetCore package, which provides the required functionality for SwaggerUI integration.

Step 2: SwaggerUI

Next, we need to configure SwaggerUI in the Startup.cs file of our ASP.NET Core Web API project. Open the Startup.cs file and locate the method. Add the following code to SwaggerUI:

services.AddSwaggerGen(c =>
    c.SwaggerDoc("v1", new OpenApiInfo { Title = "My API", Version = "v1" });
    c.SwaggerDoc("v2", new OpenApiInfo { Title = "My API", Version = "v2" });


In the above code, we are adding two Swagger documents for version 1 and version 2 of our API. You can add more as per your requirement. The SwaggerDoc method takes the version number and a description of the API as parameters.

Step 3: Set Default Version

To set the default version in SwaggerUI, we need to modify the Configure method in the Startup.cs file. Locate the Configure method and add the following code:

app.UseSwaggerUI(c =>
    c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API v1");
    c.SwaggerEndpoint("/swagger/v2/swagger.json", "My API v2");
    c.RoutePrefix = string.Empty;

In the above code, we are setting the SwaggerEndpoint for each version of the API. The first is the URL of the Swagger JSON file, and the second parameter is the display name for the version. By setting the RoutePrefix to an empty string, we ensure that the SwaggerUI is accessed directly from the root URL.

Step 4: Test the Default Version

Now that we have configured the default version in SwaggerUI, let's test it. Run the ASP.NET Core Web API project and navigate to the SwaggerUI URL (usually http://localhost:5000/swagger). You should see the SwaggerUI interface with the default version (v1 or v2) displayed.

If you want to between different versions, you can use the version dropdown provided by SwaggerUI. It allows you to select the desired version and view the API documentation.


In this article, we learned how to set the default version in SwaggerUI using ASP.NET Core Web API. By following the steps outlined above, you can enhance the API documentation and testing experience for your ASP.NET Core Web API project.

Rate this post

Leave a Reply

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

Table of Contents