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 asp.net 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,
- Visual Studio 2019
- SQL server 2017
- .Net core 3.1
- 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.
- public void ConfigureServices(IServiceCollection services)
- {
- services.AddControllers();
- services.AddDbContextPool<EmployeeContext>(options =>
- options.UseSqlServer(Configuration.GetConnectionString("EmployeeDBContextConnectionString")));
- services.AddScoped<IEmployeeService, EmployeeService>();
-
-
- services.AddSwaggerGen(swagger =>
- {
- swagger.SwaggerDoc("v1", new OpenApiInfo
- {
- Title = "Demo Employee API",
- Version = "v1.1",
- Description = "API to unerstand request and response schema.",
- });
- });
- }
- public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
- {
- if (env.IsDevelopment())
- {
- app.UseDeveloperExceptionPage();
- }
- app.UseHttpsRedirection();
- app.UseRouting();
- app.UseAuthorization();
- app.UseEndpoints(endpoints =>
- {
- endpoints.MapControllers();
- });
- app.UseSwagger();
- app.UseSwaggerUI(c =>
- {
- c.SwaggerEndpoint("/swagger/v1/swagger.json", "Demo Employee API");
- });
- }
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.
- profiles": {
- "IIS Express": {
- "commandName": "IISExpress",
- "launchBrowser": true,
- "launchUrl": "Swagger",
- "environmentVariables": {
- "ASPNETCORE_ENVIRONMENT": "Development"
- }
- },
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.
Summary
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