Reviewing the API Controllers
In this lesson, we'll go through and review the API Controllers
Get the project source code below, and follow along with the lesson material.
Download Project Source CodeTo 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.
Get unlimited access to The newline Guide to Fullstack ASP.NET Core and React with a single-time purchase.

[00:00 - 00:08] Now, as you can see here, we have some data as part of the response. If you're following along, you'll see the same data here.
[00:09 - 00:17] We can see this response because we hit the weather forecast endpoint. You can see this beautifully managed because Swagger is taking care of all this .
[00:18 - 00:27] As we discussed in the last lesson, Swagger is a documentation tool for our API . So all the endpoints we make in our project will be displayed on this page.
[00:28 - 00:37] You can run it or troubleshoot it. Currently, we only have one endpoint, weather forecast, which comes bootstra pped with our web API project.
[00:38 - 00:44] This was just the little overview of Swagger. But as mentioned, we will be using postman to test our endpoints.
[00:45 - 00:48] Now let's go back to our code. You saw the response.
[00:49 - 00:56] Now let's see why we saw what we saw. Inside the API project, we have controllers folder.
[00:57 - 01:05] In our controller folder, we have a weather forecast controller file. Now you can guess from where did the weather forecast endpoint come from.
[01:06 - 01:19] As we saw in the last lecture, we have Configure method, which has a middleware called app.use routing. Now this middleware looks at the endpoint of the HTTP request and sends it to the appropriate controller.
[01:20 - 01:26] Now, let's go inside our controller. We have all our declarations on top.
[01:27 - 01:34] Below that, we have a controller. Controllers in our web API are classes that are derived from controller base.
[01:35 - 01:43] The controller base class provides many properties and methods that are useful for handling HTTP requests. Let's take a look at some of the methods.
[01:44 - 01:50] Click on the controller base and press F12. It will take you to the controller base class.
[01:51 - 01:59] Now let's search for bad request. This error is displayed when the request received by the server cannot be processed.
[02:00 - 02:03] Let's look at another method. Let's search for not found.
[02:04 - 02:11] Here we go. Now this error indicates that the server can't find the requested resource.
[02:12 - 02:20] Now let's go back. If we hover over it, it says a base class for an MBC controller without view support.
[02:21 - 02:34] If it were an MBC project, controllers usually come with view support. Since we are only using .NET for the API, so we don't need to serve views right now, we will let React provide us the user interface.
[02:35 - 02:41] Everything inside the square brackets are called attributes. The API controller attribute makes rooting a requirement.
[02:42 - 02:54] It also displays validation errors automatically on your behalf if there is any model validation error. If the state of the model is invalid, it will automatically trigger the HTTP 400 response.
[02:55 - 03:06] Now coming to the root attribute, it has a placeholder called controller. So what it does is it checks the name of the class and removes controller from the name and make it the endpoint.
[03:07 - 03:23] For example, from the weather forecast controller class, it will omit the controller keyword and make weather forecast the endpoint as we saw in the browser. So when we hit the endpoint, if the endpoint has get method or get request, it will invoke this method.
[03:24 - 03:36] This method is responsible for sending the arrays of date, temperature and summary back to the client. Now the entities we are sending back as a response are defined inside the weather forecast.cs file.
[03:37 - 03:46] Right now it's inside the API project, but later on we will move all our models to the entity project. If we look inside this file, we have four properties.
[03:47 - 03:59] It starts with public, which means it can be accessed by the same class or by any other class in any assembly, then it has the data type followed by the name of the property. And finally, it has a getter and a set up.
[04:00 - 04:12] If you are coming from an object oriented background, you will be pretty familiar with it. Basically, they are just excesses and mutators, where get returns the value and set mutates the value.
[04:13 - 04:29] Now, if it's not written in this way, it can also be written as this get is returning the value and set is mutating the value. Now you can shape it in any way you want, but since we are just initializing the value, it will be like this.
[04:30 - 04:39] So you can try it like this or in an easy way, it's written this way. Now let's take them off.
[04:40 - 04:48] So basically, these properties are returned back to the client. And this cause we are going to make plenty of controllers.
[04:49 - 04:50] So don't worry if you are missing out on something.