Swagger API in ASP.NET Core Web API

How to Install Swagger API in ASP.NET Core Web API

In this article, I am going to discuss How to Install Swagger API in ASP.NET Core Web API. Please read our previous article where we discussed Default ASP.NET Core Web API Files and Folders i.e. the Files and Folders which are created by default when we create a new ASP.NET Core Web API Application.

Explore ASP.NET Core Web API Project

ASP.NET Core is a cross-platform, high-performance framework for building web applications and APIs. It provides a powerful and flexible way to create web APIs using the ASP.NET Core Web API project template. This template sets up a project with all the necessary components and configurations to build RESTful APIs. To explore an ASP.NET Core Web API project, you can follow these steps:

Create a new ASP.NET Core Web API project:
  • Open Visual Studio or your preferred code editor.
  • Create a new project and select the “ASP.NET Core Web API” template.
  • Choose the desired options like project name, location, and target framework.
Project Structure:
  • The project will have a typical structure with files and folders.
  • The Controllers folder contains API controllers that define the endpoints and handle requests.
  • The Models folder holds the data models used by the API.
Define API Endpoints:
  • Open one of the controllers in the Controllers folder.
  • Add methods with appropriate HTTP verbs (GET, POST, PUT, DELETE) and route attributes.
  • Implement the logic for handling requests and returning responses.
Run the API:
  • Build and run the project from Visual Studio or the command line.
  • The API will be hosted on a local development server.
Test the API:
  • Use tools like Postman or Curl to send requests to the API endpoints.
  • Verify the responses and make sure the API behaves as expected.
Explore Swagger API in ASP.NET Core Web API

Now let’s move on to exploring Swagger API. Swagger is an open-source toolset that helps developers design, build, document, and consume RESTful APIs. It provides a user-friendly interface to interact with APIs and generates interactive documentation, which makes it easier to understand and test APIs.

Swagger greatly simplifies API exploration and testing, especially when working with complex APIs or collaborating with front-end and back-end developers.

Remember to properly secure and restrict access to Swagger endpoints and documentation in a production environment to prevent unauthorized access.

Testing the ASP.NET Core Web API Project with Swagger

Now let’s move on to testing the ASP.NET Core Web API Project with Swagger:

  1. Install Swagger UI: If you haven’t already, install Swagger UI in your Web API project. Swagger UI provides a user-friendly interface to explore and interact with your API documentation. You can follow the official Swagger UI documentation to set it up in your project.
  2. Launch your Web API Project: Start your Web API project and navigate to the Swagger UI endpoint. This endpoint is usually available at /swagger or /swagger/index.html.
  3. Explore the API Documentation: In Swagger UI, you will see a list of available endpoints, grouped by tags or categories. Click on an endpoint to expand it and view the available operations (GET, POST, PUT, DELETE, etc.). Click on an operation to see the request details, including parameters, request body, and response structures.
  4. Execute Requests: To test your API, click on an operation in Swagger UI. It will display an interface to enter the required parameters and payload (if applicable). Fill in the necessary details and click the “Try it out” button.
  5. View the Response: Swagger UI will send the request to the specified URL with the provided parameters. It will display the response received from the API, including the status code, response headers, and the response body.

By using both Postman and Swagger, you can have comprehensive API testing coverage. Postman allows you to send requests and inspect responses, while Swagger provides an interactive documentation interface that makes it easier to explore your API endpoints and execute requests.

1. Install the Swagger Package in ASP.NET Core Web API:

Open the NuGet Package Manager in Visual Studio or use the dotnet CLI to install the Swashbuckle.AspNetCore package. Even it is also possible to install the Swagger API while creating a new Project. Let us proceed and see all the options that are available to install the Swagger API Package.

Method1: Enable Swagger API while Creating the ASP.NET Core Web API Project

While creating a new ASP.NET Core Web API Project, we can check the Enable OpenAPI support checkbox while providing the Additional Information which will automatically install the Swagger API in our project as shown in the below image.

Enable Swagger API while Creating the ASP.NET Core Web API Project

If you remember in our Creating ASP.NET Core Web API Project in Visual Studio 2022 article, we have checked this checkbox while creating the project and this is the reason, we are able to access the Swagger Page in our application. If swagger is installed, then you will find the corresponding Swashbuckle.AspNetCore package inside the Packages folder which comes inside the Dependencies folder as shown in the below image. Any package that we install in our Project will come inside the Packages folder.

Enable Swagger API while Creating the ASP.NET Core Web API Project

Once the Package is installed, then you need to configure the Package.

Method2: Installing Swagger API using NuGet Package Manager

Now, if you have already created the ASP.NET Core Web API Project without checking the Enable OpenAPI support checkbox, then you can also install the Swagger API using NuGet Package Manager. To do so, open the NuGet Package Manager window and then search for Swashbuckle.AspNetCore in the browse tab as shown in the below image. Then choose the Swashbuckle.AspNetCore package, select the Project where you want to install this package and finally click on the Install button as shown in the below image.

Installing Swagger API using NuGet Package Manager

Once you click on the Install button, it will open the Preview Changes window, simply click on the OK button as shown in the below image.

How to Install Swagger API in ASP.NET Core Web API

Then Accept the License by clicking on the I Accept button as shown in the below image.

How to Install Swagger API in ASP.NET Core Web API

That’s it. It will install the Swagger API Package.

2. Configure Swagger in Program.cs:

To configure Swagger API, we need to add AddSwaggerGen() service on the builder object within the Main method of the Program class as shown in the below code.

Configure Swagger in Program.cs

3. Enable Swagger Middleware:

Once we configure the Swagger API, then we need to Enable the Swagger Middleware as follows within the Main method of the Program class.

Enable Swagger Middleware

4. Run the API:

Now, we need to Build and Run the project again. And then access the Swagger UI by navigating to the URL: http://localhost:<port>/swagger as shown in the below image.

Run the API

5. Explore the API:
  • Swagger UI will display a user-friendly interface with a list of available API endpoints.
  • Click on an endpoint to expand it and view details like request parameters, response schemas, and example requests/responses.
  • You can use the provided interface to send requests and receive responses directly from Swagger UI.

The swagger will display the details of all the Web APIs available in your project. As you can see in the above image, it shows one API i.e. /WeatherForecast and the type is Get. Now click on the /WeatherForecast API to see details as shown in the below image.

Explore the API

Once you click on the /WeatherForecast API, then it will show you the details of this API as shown in the below image.

Explore the API

Now let us see how to test the API i.e. WeatherForecast API using swagger. To test the API using Swagger, first, click on the try it out button as shown in the below image.

Swagger API in ASP.NET Core Web API

Once you click on the Try it Out button, it will open the below, and again here click on the Execute button as shown in the below image.

Swagger API in ASP.NET Core Web API

Once you click on the Execute button, it will give you the response as shown in the below image. Here, you can find the request URL, the response body, the response status code, and the response headers.

Swagger API in ASP.NET Core Web API

That’s a brief overview of exploring an ASP.NET Core Web API project and integrating Swagger API documentation. In the next article, I am going to discuss How to Convert a Console Application into ASP.NET Core Web API Application. Here, in this article, I try to explain Swagger API in ASP.NET Core Web API. I hope you enjoy this Swagger API in the ASP.NET Core Web API article.

Leave a Reply

Your email address will not be published. Required fields are marked *