Integrate Swagger With .NET Core 3.1 And Entity Framework Core

Swagger is an open-source framework that will help the developer to test their Rest API using UI. Swagger provides very attractive UI representation which helps developers to build a document and test API using UI.
In this article, I will demonstrate how to integrate Swagger UI with core web API and entity frame core. Swagger UI will help developers to test their API endpoint and provide documentation. Let's start step by step with how to integrate Swagger UI in the .net API core.
Before starting Swagger integration we need to create Core API which I have demonstrated in my previous article which you can find from here CRUD Operation using .net Core API.
Before starting this there are a few prerequisites as below,
  1. Visual Studio 2019
  2. SQL server 2017
  3. .Net core 3.1
  4. Entity Framework core
  • Let's start with Integrate Swagger in the .net core API project. We need to install the below packages from the Nuget package manager to our existing API project so let's start with installing the required package and other setup.
  • Let's install Swashbuckle.AspNetCore package from Nuget package Manager. 
  • Now we need to configure AddSwaggerGen to service in Startup.cs class. Please add the below details for Swagger.
  • Please refer to the below snippet which is required to set up Swagger UI.
Lets add code in start up class as below.
  1. public void ConfigureServices(IServiceCollection services)  
  2. {  
  3.     services.AddControllers();  
  4.     services.AddDbContextPool<EmployeeContext>(options =>   
  5.     options.UseSqlServer(Configuration.GetConnectionString("EmployeeDBContextConnectionString")));  
  6.     services.AddScoped<IEmployeeService, EmployeeService>();  
  8.     //Add Swagger relates setting  
  9.     services.AddSwaggerGen(swagger =>  
  10.     {  
  11.         swagger.SwaggerDoc("v1"new OpenApiInfo  
  12.         {  
  13.             Title = "Demo Employee API",  
  14.             Version = "v1.1",  
  15.             Description = "API to unerstand request and response schema.",  
  16.         });  
  17.     });  
  18. }    
  1. public void Configure(IApplicationBuilder app, IWebHostEnvironment env)    
  2. {  
  3.     if (env.IsDevelopment())    
  4.     {    
  5.         app.UseDeveloperExceptionPage();    
  6.     }  
  7.     app.UseHttpsRedirection();  
  8.     app.UseRouting();  
  9.     app.UseAuthorization();  
  10.     app.UseEndpoints(endpoints =>    
  11.     {    
  12.         endpoints.MapControllers();    
  13.     });  
  14.     app.UseSwagger();    
  15.     app.UseSwaggerUI(c =>    
  16.     {    
  17.         c.SwaggerEndpoint("/swagger/v1/swagger.json""Demo Employee API");    
  18.     });    
  19. } 
Once you are done with the above configuration related changes in the startup class, you can do the below change in launchSettings.json file under properties folder to redirect to swagger UI page when we start API project. 
  1. profiles": {  
  2.     "IIS Express": {  
  3.       "commandName""IISExpress",  
  4.       "launchBrowser"true,  
  5.       "launchUrl""Swagger",  
  6.       "environmentVariables": {  
  7.         "ASPNETCORE_ENVIRONMENT""Development"  
  8.       }  
  9.     },  
After this change please run your API project. You will be redirected to swagger UI page with all endpoint as per the below screen method for each controller list in UI. Please have a look at the below screen for UI.
Test Get method "/api/Employee/GetEmployees" from UI
Click on Execute button to execute your methods to see the desired result of the above action method. As of now, we have three records in our database which will display in the below screen.

Let's add one more record using "/api/Employee/AddEmployee". Add required detail in RequestBody and click on Execute.
After clicking on Execute button, AddEmployee method return with Success code 200 and records added to database table. 
Check if the get "/api/Employee/GetEmployees" end point above employee is added in database or not.

Above employee is added to the table successfully which we are able to see in the above snippet with red border marked. This way we can test remaining methods with proper RequestBody.


In this article, we learned about how to integrate swagger UI to core API 3.1 and test API using UI. Swagger is popular for its swagger UI and that helps developers to test their end point. Please read my other article on Web API, .Net Core, Swagger UI, Elastic Search, SQL Server Click Here