.Net Core web API - Part 1 - Swagger

Oct 12, 2019

titulo

In this series, I will explain how to create a .Net Core 3 web API connected with a database and deployed to a remote server. The first step is to integrate it with Swagger by using the Swashbuckle package.
The full example can be downloaded in this GitHub repository: NetCore3-Swagger.

Technologies

Topics

.Net Core 3

Download the Visual Studio 2019 here and install it.

Create a new project:

netcore01

Choose ASP.Net Core Web Application:

netcore02

Name the project and configure the local path:

netcore03

Select an empty .Net Core project and uncheck the HTTPS and Docker support options:

netcore04

After the solution be compiled, create a folder named “Controllers” and add into it a class named “TestController” with an async method:

netcore07

Now, let’s configure the “Startup” class:

netcore08

Run the project in debug mode and access the URL below:

http://localhost:<API-PORT>/api/Test

If the “OK!” message be displayed in the browser, our web API is running!

Swagger

Inside the Visual Studio 2019, open the Package Manager Console and run the following commands to install the Swashbuckle version compatible with .Net Core 3:

Install-Package Swashbuckle.AspNetCore -Version 5.0.0-rc3
Install-Package Swashbuckle.AspNetCore.Filters -Version 5.0.0-rc3
Install-Package Swashbuckle.AspNetCore.Annotations -Version 5.0.0-rc3

Open the “Startup” class again and modify the “ConfigureServices” method:

swagger01

And the “Configure” method as well:

swagger02

Open the project’s properties window, go to “Debug” and type “swagger” inside the input field above the environment variables list:

swagger04

Inside the “TestController”, put a simple annotation above the “Get” method:

swagger03

Now, let’s run the project again. The Swagger page will open in this URL:

http://localhost:<API-PORT>/swagger/index.html

This will be the result:

swagger08

Try the “Get” method and the “OK!” message will return from the API.

swagger09

Conclusion

In 10 minutes, you were able to create a .Net Core 3 web API with Swagger. In the next chapter, we will connect it with a remote MySQL database.


Categories:
Tags: