Net Core 2 — Creating a simple Web API with Visual Studio Code on Linux Ubuntu

Nicola La Rocca
5 min readMay 1, 2019

--

Introduction

In this short tutorial we’re going to create a simple web service using Microsoft .Net Core 2 and Visual Studio Code on Linux Ubuntu OS

Tutorials requirements

  1. PC or VM with Linux Ubuntu 18.04 (or Windows)
  2. Microsoft Visual Studio Code
  3. Microsoft .Net Core 2.1

Creating a new Web Api Project

Step 1 Check dotnet core

First of all, we need to check the presence of .Net Core 2 in our Linux PC opening a new linux console and using the following command. We need a version greater than 2.0

Step 2 Creating a new web api project

Inside the folder where project will be created, write this statement to create a new web API project named TestWebApi

After a few seconds we’ll see these information

Step 3 Open project with Visual Studio Code

Open Visual Studio Code, click on File → Open Folder… and select our new web API folder. Wait a few seconds and you’ll see every project’s elements.

Opening Startup.cs we can see that dotnet have created the main configuration code ready to be used.

Inside the Controllers folder we’ll find ValuesController class. This is a simple class to test immediately our web API but, in this tutorial, we won’t use it. If you want, delete it.

Step 4 Create Models Folder

It’s time to create our model layer. We have to click on startup.cs and than we have to click on “new folder” icon naming it “Models”. This folder will contain every model classes of our project.

Step 5 Create Products class

Now we’ll create our first model class. Normally, model class permit to map DBMS tables with entity framework. In this tutorial we won’t use a DBMS (we’ll make it in a second tutorial). We have to create a new class naming it Products.cs and we have to write the following code:

This class contains all the properties we can get from a relational database like MySQL or PostgreSQL but, in this tutorial, we’ll insert data using a specific method without connecting app to a DBMS.

Step 6 Create Products Controller Class

Now it’s time to create our first controller class. Inside Controllers folder, we create a new class named ProductsController.cs and we have to write the following code:

At the top of the class, we wrote two attributes which specify the base route of our service [api/products] and the format type (json) returned by API.

After that we’ve created an array of Products classes populated with data of four Italian grocery article (products description not translated, sorry!)

The first method writed in the class (listAllProducts) will use the base route and the HTTP get method ([HttpGet]) to return all products data inserted inside the products collection.

Inside the class we’ve created other two methods to allow the search for products using code and description.

The second method ListProductsByCode use HTTP GET method to search a product using its identification code. Note that we used “code/{codart}” as [HttpGet] argument to specify the additional part of URL and the “codart” variable that has the same name of method’s variable.

In the body of method we used Linq syntax to select and filter products collections by codart “where g.Code.Equals(codart)”

The third method ListProductsByDescription is similar to the second one, but has a different [HttpGet] argument and use Linq syntax to select, order and filter products collection by description.

At this point, our controller class is ready to be used and we can test it using Postman.

Step 7 Manual Testing with the use of Postman

First of all, we have to press F5 key and wait until the project will be started

If we have no errors, our web API will be listening on http://localhost:5000 and will be ready to be tested.

Open Postman, select HTTP method GET and write base URL http://localhost:5000/api/products. Press Send button and you’ll get the following result

Now modify URL writing http://localhost:5000/api/products/code/007686402 and you will get the following result:

With the third test you’ll use the following the URL http://localhost:5000/api/products/description/barilla and you’ll get the following result:

Congratulation! We’ve just created our first Web Api using .Net Core 2 on Linux Ubuntu.

Conclusion

In this tutorial we started to create our first web service REST using visual studio code and .net core 2 on linux Ubuntu. In the next tutorial we’ll use data inside a DBMS and we’ll test Entity Framework ORM.

--

--

Nicola La Rocca
Nicola La Rocca

Written by Nicola La Rocca

Java, C# and SQL Developer. Udemy Instructor

Responses (2)