Using Swagger in MVC core

Tohid haghighi
1 min readMay 13, 2021

--

Swagger tooling for APIs built with ASP.NET Core. Generate beautiful API documentation, including a UI to explore and test operations, directly from your routes, controllers and models.

In addition to its Swagger 2.0 and OpenAPI 3.0 generator, Swashbuckle also provides an embedded version of the awesome swagger-ui that’s powered by the generated Swagger JSON. This means you can complement your API with living documentation that’s always in sync with the latest code. Best of all, it requires minimal coding and maintenance, allowing you to focus on building an awesome API.

And that’s not all …

Once you have an API that can describe itself in Swagger, you’ve opened the treasure chest of Swagger-based tools including a client generator that can be targeted to a wide range of popular platforms. See swagger-codegen for more details.

Step 1 -Add swagger package with Nuget :

Package Manager : Install-Package Swashbuckle.AspNetCore -Version 6.1.4
CLI : dotnet add package --version 6.1.4 Swashbuckle.AspNetCore

Step 2 -Add swagger in services:

services.AddMvc();

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

Step 3 -Add swagger in middlewares :

app.UseSwagger();
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("v1/swagger.json", "My API V1");
});
swagger api

--

--

Tohid haghighi
Tohid haghighi

Written by Tohid haghighi

Full-Stack Developer | C# | .NET Core | Vuejs | TDD | Javascript

No responses yet