Adding our First Controller

In this lesson, we'll add our first controller which will get us data from our database

Project Source Code

Get the project source code below, and follow along with the lesson material.

Download Project Source Code

To set up the project on your local machine, please follow the directions provided in the README.md file. If you run into any issues with running the project source code, then feel free to reach out to the author in the course's Discord channel.

This lesson preview is part of the The newline Guide to Fullstack ASP.NET Core and React course and can be unlocked immediately with a single-time purchase. Already have access to this course? Log in here.

This video is available to students only
Unlock This Course

Get unlimited access to The newline Guide to Fullstack ASP.NET Core and React with a single-time purchase.

Thumbnail for the \newline course The newline Guide to Fullstack ASP.NET Core and React
  • [00:00 - 00:10] We have successfully seeded the data in our database and we can see it in our table. Now we want to query the database and for that they will have to write our controller.

    [00:11 - 00:20] First of all let's open the API project and check what's going on inside the weather for Cuz controller. In this project we are going to create a lot of controllers.

    [00:21 - 00:37] All of them will derive from the controller base and will share the similar attributes such as API controller attribute and root attribute with controller placeholder . Since we are sharing this part in all our controllers rather than repeating ourselves in each controller.

    [00:38 - 00:53] Let's create a base controller which will include all of this and we will derive from that. Let's create a new class and name it base controller.

    [00:54 - 01:09] As mentioned earlier we will derive it from controller base which is same as we saw inside the weather forecast controller. Let's use quick fix and import it from Microsoft.asp netcore.mbc.

    [01:10 - 01:14] One step is done. Let's also use both the common attributes above this.

    [01:15 - 01:25] API controller attribute and the root attribute with API/controller placeholder . Since we want our endpoints to start with the API root.

    [01:26 - 01:37] This base holder will be replaced with the name of the class which we are going to name courses controller and the root will be called API/courses. Let's create the class now.

    [01:38 - 01:47] Right click on the controller folder and click on new class. We are going to call it courses controller.

    [01:48 - 01:56] Let's derive it from the base controller we just created. We will be using services inside the controller for which we will have to create a constructor.

    [01:57 - 02:06] Let's put the cursor on courses controller and use quick fix. Choose generate constructor courses controller.

    [02:07 - 02:13] A constructor is now ready. To query a database we need store context here.

    [02:14 - 02:27] This is not going to be part of our architecture but just for the time being. Let's call this store context here and let's call it context.

    [02:28 - 02:39] Let's bring it using infrastructure. To make this context available for all our methods let's go to context and use quick fix.

    [02:40 - 02:46] Let's select initialize field using parameter. I'm telling you again that this practice is not recommended.

    [02:47 - 02:54] We are doing it just for the time being. We will abstract the database using repository pattern in the future lessons.

    [02:55 - 03:01] We are using this method just for demonstration purpose. Below it let's create our get method.

    [03:02 - 03:10] Let's write HTTP get inside the square brackets. Below that let's write a method.

    [03:11 - 03:25] Public async task. Return type is going to be action result of list and the list will be of type cause.

    [03:26 - 03:30] Let's call this method get courses. We see a lot of reds here.

    [03:31 - 03:39] Let's resolve them using the quick fix. Let's call HTTP get using Microsoft dot ASP net core dot MBC.

    [03:40 - 03:53] Let's import task from system dot threading dot tasks. List will be imported from system dot collections dot generic and course from our entity project.

    [03:54 - 04:00] Now let's go inside the method and let's return our data. We are going to return the list of our courses.

    [04:01 - 04:10] Let's write it down here. await_context dot courses dot to list async.

    [04:11 - 04:16] Let's import it from entity framework core. There is nothing much going on here.

    [04:17 - 04:27] We are just returning the data as it is from the database. Let's create another get method but this time it will take a root parameter called ID.

    [04:28 - 04:38] This method will return the course with the particular ID. If you want to pass an argument inside the HTTP attribute you need to pass it inside curly brackets.

    [04:39 - 04:45] The end point in this case will be courses slash ID. Let's write a new method.

    [04:46 - 05:05] Public async task action result of type cause since we are looking for a single cause with the particular ID. Let's call this method get cause and inside the parameters we want to mention the ID which is of type GUID.

    [05:06 - 05:21] Let's bring GUID using system. Inside this method let's return await_context dot courses dot find async and we 'll mention the ID.

    [05:22 - 05:31] And async method will give us the cause with the ID we mentioned inside this method. To make sure things are updated let's restart the server.

    [05:32 - 05:43] Although it's not necessary but we'll do it this time. Now let's go back to postman and select get courses request from module 1.

    [05:44 - 05:51] You must be wondering what exactly is URL here. This is a variable which we can store inside our request.

    [05:52 - 06:06] If we go to the right hand side of the learnify collection and click on the three dots and click on edit go to variables tab. We can see our URL variable with initial value and current value.

    [06:07 - 06:13] Both set to localhost 5000. Since we are using this endpoint for development stage.

    [06:14 - 06:22] You can change the variable if you are using any other endpoints such as HTTPS 5001. Let's make the request now.

    [06:23 - 06:31] Ah oh we get a 404 not found error. And I might know the reason why.

    [06:32 - 06:39] Let's go back to our code and check our piece controller. And yes I know where I did the mistake.

    [06:40 - 06:46] Did you notice the error as well? I forgot to add the square brackets around the controller placeholder.

    [06:47 - 06:53] Which means there was no endpoint called api/courses. And we were hitting a random URL.

    [06:54 - 07:03] Let's place the square brackets now and save the code. Now let's go back to postman and rerun the request.

    [07:04 - 07:14] And yes it says 200 OK and returns the data which we seeded in our database. Let's now pick the ID from any of these courses.

    [07:15 - 07:22] Use it for get course method. Replace the ID here since it's going to be unique.

    [07:23 - 07:28] Now press send. We see the course with this particular ID perfect.

    [07:29 - 07:37] We have achieved what we expected from this module. Next we will show these courses to the client for which we will be using react.

    [07:38 - 07:40] So let's tackle our front end from the next lecture. Bye bye.