Create an ASP.NET Core WebAPI with the dotnet cli and Visual Studio Code

To build a quick backend solution which we can communicate with can sometimes be challenging and takes a lot of time. In this course we will learn how we can create an ASP.NET Core WebAPI with the dotnet command line interface and how we can query our first data from that REST endpoint.

You must be a Member to view code

Instructor: 00:00 To create a new ASP.net Core web API, we can start typing .net new to see all the templates of the .NET command line interface. One template is the web API template, which we will use in this course.

00:15 To use it, we can type .net new web api, and this will scaffold all files for a new web API project inside the current folder. Let's take a look at all the files which have been created so far.

00:30 First of all, there's a program.cs file, which represents the entry point of our application. You will notice that a web API is nothing else than a console application, having a method like public static void main taking some arguments.

00:46 All the method does is starting up a web server, reading a configuration file, and using a startup class to configure all the services and the pipeline for applications.

00:58 Let's take a look at the startup.cs file. As we configure the web API in the first step, the configuration gets automatically passed into the constructor of this class. If you read a jsed file from your system which holds your configuration, you can access it via the configuration property inside the startup class.

01:19 Other methods are the configure services method and the configure method. The configure services method gets passed an IServiceCollection, which we can use to register our services to the built-in dependency injection system from ASP.NET Core.

01:35 Here, we can choose between three modes. AddSingleton to register singleton services, AddScoped to have an instant of the service as long as the HTTP request runs, and AddTransients to get a new instance every time we inject the service in our application. In this case, we will only add the required MVC service to our dependency injection system.

02:00 The configure method creates the pipeline for HTTP requests. When this pipeline is built, an HTTP request runs through the whole pipeline where we can add specific middleware if we want our HTTP request to run through that. In this case, we only add MVC to our request pipeline.

02:20 Let's head over to our controller, where our HTTP request can be processed. The controller is a normal class which inherits from controller. The first thing you will notice is the route attribute on top of the class. This represents the string which we have to send our calls to if we want to hook into this particular controller.

02:41 The next thing you notice is the controller in those brackets. This will get replaced with the name of the class without the suffix controller. In this case, the route to reach the controller is API/values.

02:56 Inside this controller, we can mock our methods with the corresponding HTTP webs like HTTP GET, and we will jump in that method. We can have an HTTP GET and pass a parameter. We can have HTTP POST, which reads the body as a parameter inside that method. We can have HTTP PUT and DELETE.

03:21 Let's run this web API to see if this is working. To run it, we can simply head over to the console and type .net run. This will start a new web server for us and make the API available under localhost port 5000.

03:37 If we head over to our browser and hit localhost:5000/api/values, we can see the JSON response value 1 and value 2. Great, that's working. We can tweak our API a little bit. Let's get back to the code.

03:55 In this get all method, we are very restricted to returning an i number of a lost string. We could not give back something else, and it's not transparent which status code is coming back. To improve that, we can change the return type to IActionResult, which is more generic and helps us making our lives easier.

04:15 To fulfil that IActionResult interface, we can use a small helper method which is called OK, take in an object as a parameter. Now, through this method, we do return a 200OK status code, and as a body of the response, we're still sending back a string array. Let's adapt that to the other methods too.

04:35 The GET method take in a parameter will just return the value we pass it. The POST method is returning and created an action which is a 201 created status code. Here, we can pass the name of the action where the item was created and the item itself in the body.

04:58 The link will be sent in the response header. In the body, we have the just created object. In this case, we will return the value we passed from the request body.

05:09 The PUT method will return an OK status code, and the delete method returns a no content, which is 204. Let's see if that still works.

05:25 We open up our browser, and if we now call GET with the value, we get back the number we passed. That works. Let's see if we can get back all the items too. Great, that works as well.

05:38 Back in the code, we can also show how to work with objects instead of only strings. Therefore, we will create a new folder called models and create a class called customer. The customer just has a name and an age property on it.

05:51 Back in the controller, then we can instead of only returning strings, create two customers, for example, saying Phil with the age of 60, and Mike with the age of 61, and pass that OK method a new list containing those customers.

06:12 If we start our API again, head over to our browser, we can see that the API automatically serializes our classes to JSON, and the two customers are coming back from our GET method. This are the first steps to create a web API in ASP.NET Core with the .NET CLI and Visual Studio code.