Introduction
Swagger UI is very powerful documentation tool for Restful services. It is a framework for describing and consuming RESTful APIs. It keeps the documentation system of methods, parameters, and models tightly integrated into the server code. We can add Swashbuckle to any Web API project, and then we can easily customize the generated specification along with the UI (swagger-ui). Swagger UI uses Swashbuckle to displays the documentation.
Description
In this article, I will show the steps to add Swagger in Web API Application to document and test restful Web API services.
- Create Asp.net Web API project using SQL Server
- Add Swashbuckle to Asp.net Web API project and displays the Swagger UI documentation
Before going through this article, visit my previous articles as mentioned below.
- Part 1 - ASP.NET Web API Using MVC And Entity Framework
- Part 2 - ASP.NET Web API Using MVC, Entity Framework And jQuery For Retrieve Data
- Part 3 - Reuse The Model Classes Of Entity Data Model (.edmx) To Multiple Projects Using Class Library In ASP.NET Web API
- Part 4 - ASP.NET Web API Using MVC, Entity Framework And HttpClient For Retrieve Data
- Part 5 - ASP.NET Web API Using MVC, Entity Framework And jQuery For Get and Post With Validation
- Part 6 - ASP.NET Web API Using MVC, Entity Framework And HttpClient For Get And Post With Validation
- Part 7 - ASP.NET Web API Using MVC And jQuery To Upload And Download Files
- Part 8 - ASP.NET Web API Using MVC And HttpClient To Upload And Download Files
- Part 9 - ASP.NET Web API Using MVC And jQuery For Paging
- Part 10 - CRUD Using ASP.NET Web API In MVC - Part Ten
Steps to be followed,
Step 1: Create a table in database
Create the EmployeeDet table and populate it with sample data as shown here.
CREATE TABLE [dbo].[EmployeeDet](
[ID] [int] IDENTITY(1,1) NOT NULL,
[FirstName] [nvarchar](50) NULL,
[LastName] [nvarchar](50) NULL,
[Gender] [nvarchar](50) NULL,
[Salary] [int] NULL,
PRIMARY KEY CLUSTERED
(
[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET IDENTITY_INSERT [dbo].[EmployeeDet] ON
GO
INSERT [dbo].[EmployeeDet] ([ID], [FirstName], [LastName], [Gender], [Salary]) VALUES (1, N'Mark', N'Hastings', N'Male', 60000)
GO
INSERT [dbo].[EmployeeDet] ([ID], [FirstName], [LastName], [Gender], [Salary]) VALUES (2, N'Steve', N'Pound', N'Male', 45000)
GO
INSERT [dbo].[EmployeeDet] ([ID], [FirstName], [LastName], [Gender], [Salary]) VALUES (3, N'Ben', N'Hoskins', N'Male', 70000)
GO
INSERT [dbo].[EmployeeDet] ([ID], [FirstName], [LastName], [Gender], [Salary]) VALUES (4, N'Philip', N'Hastings', N'Male', 45000)
GO
INSERT [dbo].[EmployeeDet] ([ID], [FirstName], [LastName], [Gender], [Salary]) VALUES (5, N'Mary', N'Lambeth', N'Female', 30000)
GO
INSERT [dbo].[EmployeeDet] ([ID], [FirstName], [LastName], [Gender], [Salary]) VALUES (6, N'Valarie', N'Vikings', N'Female', 35000)
GO
INSERT [dbo].[EmployeeDet] ([ID], [FirstName], [LastName], [Gender], [Salary]) VALUES (7, N'John', N'Stanmore', N'Male', 80000)
GO
SET IDENTITY_INSERT [dbo].[EmployeeDet] OFF
GO
Step 2: Adding ADO.NET Entity Data Model to retrieve data
First, I have created the Web API project named WebAPIProj. Next, I have created the Class Library Project named EmpDataAccess. Right click on EmployeeDataAccess project and select Add - New Item. In the "Add New Item" window Select "Data" from the left pane. Select ADO.NET Entity Data Model named EmpModel. On the Entity Data Model Wizard, select "EF Designer from database" option and click next. On the next screen, click "New Connection" button to provide SQL Server connection details and select proper database name where the EmployeeDet table is available. Then select Entity Framework 6.x and choose "EmployeeDet" table and click Finish. Now I can see the interface of "edmx" as shown below.

Please make sure the EntityFramework package is installed or not from NuGet Package Manager as shown below.

Step 3: Using the Entity Data Model in WebAPIProj project.
Using the Entity Data Model in WebAPIProj project. Right click on the references folder in the WebAPIProj project and select "Add Reference". On the "Reference Manager" screen select "EmpDataAccess" project and click OK. Now we can see that reference or DLL file as shown below. Next, we should build our solution.

Step 4: Adding Web API Controller
Now, I have created the controller named EmployeeController.cs as shown below.

Add following code in EmployeesController.cs.
Code Ref


















Join the conversation! Your thoughts help the community grow.