create a swagger example Asp.net core web application.
Microsoft Visual Studio 2019 |
Once the project is created let's add a new controller. I have added HomeController. Once the Controller is created add the following NuGet packages into the application,
Swashbuckle.AspNetCore
Swashbuckle.AspNetCore.Swagger
Swashbuckle.AspNetCore.SwaggerUI
Configuring the Swagger Middleware
Now it's time to configure services inside a startup.cs class. Open startup.cs class and add the below line of code into configuring services method.
given below :
public void ConfigureServices(IServiceCollection services) {
// Register the Swagger or more Swagger documents
services.AddSwaggerGen(c => {
c.SwaggerDoc("v1", new OpenApiInfo {
Title = "SwaggerDemoApplication", Version = "v1"
});
});
services.AddControllers();
}
Add the below line of code inside configure method. Basically we are going to enable middleware for swagger UI.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env) {
// Enable middleware Swagger for JSON endpoint.
app.UseSwagger();
app.UseSwaggerUI(c => {
c.SwaggerEndpoint("/swagger/v1/swagger.json", SwaggerDemoApplication V1 ");
});
}
Now it's time to check the result. Run the application and navigate to https://localhost:44338/swagger/Index.html.