ASP.NET Web API With Swagger

The ASP.NET Web API is an extensible framework for building HTTP based services that can be accessed in different applications on different platforms such as web, windows, mobile etc. It is just like a Web service or WCF service but the exception is that it only supports HTTP protocol.

So now, the query is when to use Web API

Use the Web API when you want to build a service that supports only HTTP protocol, to build RESTful services.

Swagger is an API description format for REST APIs. Swagger is a set of open-source tools which allows you to build, design, document, and consume REST APIs.

Let’s see it practically.

Create a new project.

ASP.NET

Select ASP.NET Web Application (.NET Framework).

ASP.NET

Now, we have different project templates and for API, we have to select Web API.

ASP.NET

  • Empty: without any default files.
  • Web Forms: Asp.net forms
  • MVC: Default MVC architecture with files
  • Web API: API controller file with MVC architecture.

ASP.NET

HelpPage is also made to test the API. You can run directly and check the help page to test the Web API and the response. But there is another powerful tool to check and test the API, named Swagger. Now, we will install Swagger which is a very powerful tool for Web API.

There are two ways of installing swagger.
  1. NuGet Package Manager Console
    Open NuGet Package Manager Console from Tools > NuGet Package Manager > Package Manager Console and in the console window, execute the following command,

Install-Package Swashbuckle –version 5.2.1

  1. From Reference
    Right click on Reference > select Manage NuGet Packages > In new window select “Browse” > Search by “Swagger” and click on "Install".

After the package is installed, navigate to App_Start in the Solution Explorer. You’ll find a new file called SwaggerConfig.cs.

Just Run an application and in the URL, append "/swagger". You will find a swagger for testing the API. The Swagger metadata can be used to tell other APIs how to interact with yours.

The video form of this article is available here.


Similar Articles