Part 1 - The API
Disclaimer:
If you are an Asp .NET MVC developer I advise you to read my previous blog post to learn why you should switch to ASP .NET web API
Getting Started
Needed Software :
- ASP.NET Core Runtime 5.0.0 (for windows users install the Hosting Bundle)
Needed VS Code Extensions :
- C# Extensions ( install the one that says "Project No longer Under Development")
- C#
- NuGet Package Manager
- Material Icon Theme
Needed NuGet Packages :
Click on Microsoft.EntityFrameworkCore and then Select the version matching your .NET version(in our case it is 5.0.0) :
Creating The Project :
1. Creating The Solution
and then we should see in our folder explorer a new generated folder called API that looks similar to the following :
Now that we've created our project, it's time to start coding something, the first step to do here is to create a C# class, to do that right click on the Controllers folder, click on New C# Class and this is what we should get :
for the sake of simplicity I wont dive deep into this and explain everything, all you need to know is the attribute [ApiController] means that this is an Api controller, and the [Route()] holds in its parameter the link we must hit to target this controller , just copy everything I did and keep in mind that in every Asp .NET Web API project you will need to do the same. Now we will create a method that will be executed every time we send a Get request to our server, a GET request is basically that we're asking the server to provide us some data and if we send the right request we will get what we asked for, the return type of this method should be an ActionResult and for test purposes we will return a message saying controller is working, the code should look like this
the next step is to create another folder called Data inside our API folder and to do that right click the API folder > New Folder and name it Data, now Right click the Data folder > New C# Class and name it TasksDbContext and your folder explorer should look like this :
now we need to add the following boilerplate which will tell our app that this class is a Db Context that holds a list of Task, so fill in as the following :
Adding Sqlite to our App
Creating the GET Action
Creating the POST Action
as you can see in the screenshot above, we have our Postman interface, we have written the url to our API "https://localhost:5001/tasks" and we have set the request type to be a post request, an additional step is that we need to add a header Content-Type with the value Application/json . What that will do is tell our API that our body is json formatted, now we need to specify the body which is a Json object that should look like the following :
as you could see in the request above we actually added that Task object into our database, and a todo list is about adding tasks and removing them when finished, but now we can only add tasks, so let's code the deleting part.
Creating the DELETE Action
as we see here we modified the URL and added /1 which is the id of our first task, and we also changed the request type to DELETE, as we click on send we receive a message saying that the task is deleted, now we need to send another get request to check if the task is deleted or no, and voila
our response is now an empty array which is what we're supposed to get, and with that we finished our simple API.
HOMEWORK
Hint 1 : the Request type of this method is called PUT
Hint 2 : the method will receive 2 paramaters (int id, Task task)
Hint 3 : you must look for the desired item through the id and linq syntax and then modify it
8 Comments
This comment has been removed by the author.
ReplyDeleteI mentioned that after opening the project folder in vs code you would install the packages
DeleteWhen second part?
ReplyDeleteNice Work ;)
It should be up this week :D thanks for commenting :)
DeleteThank you for discussing this very useful article. I heard something new from you. Keep blogging. full stack developer course in jalandhar
ReplyDeleteI know I am late, but I would like to inform you that you helped me big time. Thanks for the valuable and clear instructions.
ReplyDeleteCar Rental Auckland
ReplyDeleteCar Rental Wellington
Great Prices on Rental Cars in Auckland, Hamilton, Wellington & Christchurch - Call Now 0800 566 701 or Book Online. Get everyday low prices on quality rental cars. Bargain Rental Cars is 100% Kiwi Owned and Operated and trusted for over 20 years.
This article content is really unique and amazing. This article really helpful and explained very well. So i am really thankful to you for sharing keep it up
ReplyDeleteJava Course for Beginners - Full Stack Developer Course
Best MIS Training in Delhi - 100% Placement after Training