WEBVTT

00:00.000 --> 00:04.950
Okay, so when we created a new web API type of project, this is what it gave us.

00:04.950 --> 00:06.570
It gave us this controllers folder.

00:06.570 --> 00:08.220
Please make sure you have this.

00:08.250 --> 00:12.690
If you do not have this at this point, then that means you created a minimal API.

00:12.690 --> 00:17.250
And if you click on your program class, you're going to see something a bit different to what I'm displaying

00:17.250 --> 00:18.870
inside here.

00:18.870 --> 00:24.360
So if you do not have this controllers folder you do not have or you have different code inside the

00:24.360 --> 00:30.120
Program.cs class, then please delete what you currently have and start again.

00:30.120 --> 00:33.090
Literally create a solution file and the API project.

00:33.090 --> 00:36.180
Again, it's the quickest way to get to where you need to be.

00:36.180 --> 00:42.000
In this case, you don't want to mess around with the provided code like things in the program class

00:42.000 --> 00:44.040
at this stage if you don't have to.

00:44.490 --> 00:47.250
So let's take a look at what we have inside our project.

00:47.280 --> 00:47.490
Now.

00:47.490 --> 00:52.230
Every dotnet API project has a program class.

00:52.230 --> 00:57.690
This is the entry point to our application and is an important class in our application.

00:57.720 --> 01:02.610
As part of this lesson, we're also going to simplify things to the bare minimum and anything that we

01:02.640 --> 01:06.090
don't need right now, we're also going to remove as well.

01:06.270 --> 01:09.420
Now this is a net nine version of this project.

01:09.420 --> 01:15.240
And in prior versions of Net they used to provide swagger a way of providing documentation about our

01:15.240 --> 01:16.200
endpoints.

01:16.260 --> 01:18.150
Swagger if you've heard of it.

01:18.180 --> 01:23.280
If you haven't, don't worry about it because it's no longer maintained and Microsoft no longer include

01:23.280 --> 01:26.640
it with the templates that we use when we create a new project.

01:26.940 --> 01:33.960
What they've provided instead is open API, and this is something that you would need to configure to

01:33.990 --> 01:36.510
get the kind of swagger functionality that you had before.

01:36.510 --> 01:38.640
So we're not going to use it in this training course.

01:38.670 --> 01:45.900
My goal isn't to provide everything about everything, but a lot about a lot, and some things we're

01:45.900 --> 01:46.650
not going to cover.

01:46.650 --> 01:51.690
And this open API is not going to be covered on this training course, so I'm simply going to remove

01:51.690 --> 01:52.230
it.

01:52.350 --> 01:56.970
Also inside this program class we have two sections really.

01:57.000 --> 02:05.430
We have services things that we add to our projects that we wish to inject into other classes as and

02:05.430 --> 02:06.270
when we need them.

02:06.270 --> 02:12.780
These are referred to as services inside this class and they will go inside effectively.

02:13.140 --> 02:21.150
Consider this part a container for our services, and we'll add them as we go along.

02:21.150 --> 02:22.590
And we'll be adding many to them.

02:22.590 --> 02:25.380
And then we build our application.

02:25.380 --> 02:28.500
And then we've got this HTTP request pipeline.

02:28.500 --> 02:31.590
And this is often referred to as middleware.

02:31.980 --> 02:44.490
So our API and any API really its job is to receive HTTP requests and return HTTP responses as a request

02:44.490 --> 02:49.890
comes into our API server and it goes through what is referred to as a pipeline.

02:49.920 --> 02:58.410
You can think of it as a pipeline, and anything that we add inside this section effectively is referred

02:58.440 --> 02:59.520
to as middleware.

02:59.520 --> 03:06.220
It's software that can do something with that request as it comes in through the pipeline and as it

03:06.220 --> 03:07.990
goes out of the pipeline.

03:07.990 --> 03:11.140
Back to the client that made the HTTP request.

03:11.170 --> 03:18.430
So any middleware and we'll talk about this more as we use this stuff gets added in this part of the

03:18.430 --> 03:19.780
program class.

03:19.810 --> 03:22.210
Now we can check to see which mode we're running in.

03:22.210 --> 03:23.980
And this mode is development.

03:23.980 --> 03:26.560
It's going to use Map Open API.

03:26.590 --> 03:28.480
Once again we're not using it.

03:28.480 --> 03:32.260
So I'm actually going to remove this code from the program class.

03:32.380 --> 03:36.040
Like I say we're going to get things as simple as we can do.

03:36.070 --> 03:38.230
We're not going to use Https redirection.

03:38.230 --> 03:41.530
We're going to start our application using Https.

03:41.530 --> 03:43.330
And we're just not going to redirect it.

03:43.330 --> 03:45.040
So I'm going to remove that.

03:45.040 --> 03:47.890
And we're not going to touch authorization yet.

03:47.920 --> 03:49.780
We're going to come back to that later.

03:49.840 --> 03:55.450
So in the interests of keeping things simple I'm just going to remove that for the time being as well.

03:55.450 --> 03:58.810
So this is what your program class should look like at the moment.

03:58.810 --> 04:02.770
So when our application starts up, it runs this code.

04:02.770 --> 04:04.900
And then this line the app run.

04:04.900 --> 04:10.600
This is responsible for actually running our application and getting us to list stage that we see in

04:10.600 --> 04:13.750
our terminal after we run our application.

04:13.840 --> 04:19.120
So that's the program class, an important class that you'll see inside any web API project.

04:19.120 --> 04:23.470
I'm just going to run through briefly a few other elements inside here.

04:23.470 --> 04:26.380
Another important thing that we need to look at.

04:26.470 --> 04:29.110
And I'm inside the explorer view.

04:29.140 --> 04:31.810
There's two views that we need to think about in VS code.

04:31.840 --> 04:34.690
This one here is the File Explorer view.

04:34.690 --> 04:40.030
But in the Solution Explorer view we get more dotnet type functionality.

04:40.300 --> 04:47.350
So in the Solution Explorer view if I click on API then this takes me to my API Csproj file.

04:47.380 --> 04:50.740
Now there's a couple of things I want to point out inside here.

04:50.950 --> 04:55.390
Any packages that we add that help us develop our code inside our project.

04:55.420 --> 04:58.000
Get added into the project file.

04:58.000 --> 05:02.770
And one of the things that's included with this template is open API.

05:02.800 --> 05:04.840
I've removed the code from the program class.

05:04.840 --> 05:09.700
I don't need it inside here either, so I'm going to remove it from there.

05:10.270 --> 05:11.830
Now we've got the target framework.

05:11.830 --> 05:13.870
This says dotnet nine for myself.

05:13.870 --> 05:18.400
And we've got a couple of flags here nullable and implicit usings.

05:18.400 --> 05:24.280
I just want to briefly mention nullable, because this is quite important about how we deal with something

05:24.280 --> 05:29.290
that's referred to as nullable reference types and the nullable reference types.

05:29.290 --> 05:34.900
If I just pick on this weather forecast class, then we've got a couple of properties inside here.

05:34.900 --> 05:40.870
We've got a data only type of property, an integer property, and the string property.

05:40.900 --> 05:47.260
Now when we use nullable reference types then a string is a reference type in.

05:47.500 --> 05:56.260
Net, an integer is referred to as a value type, and the difference between the two is that the value

05:56.260 --> 05:58.240
type always has to have a value.

05:58.270 --> 06:00.160
An integer can't be null.

06:00.190 --> 06:08.510
It'll either be zero or another value, but the string traditionally how Net has handled this is strings

06:08.540 --> 06:09.260
could have been null.

06:09.260 --> 06:16.010
But when we've got this nullable flag enabled, then if I say that the string is no longer optional.

06:16.010 --> 06:19.220
And that's what this question mark means here, it means that it can be null.

06:19.220 --> 06:24.860
Then I'm going to get a warning from net, and it tells us that Non-nullable property summary must contain

06:24.860 --> 06:27.800
a non-null value when exiting the constructor.

06:28.490 --> 06:30.710
So what this means it's a kind of good thing that.

06:30.920 --> 06:33.740
Net has added in recent versions of Net.

06:33.860 --> 06:39.920
It prevents us from creating properties that we do not know if they should or should not be null.

06:39.950 --> 06:45.650
So if we tell Net that this can be null, then it's going to warn us if we try to use it.

06:45.680 --> 06:50.840
If we haven't checked for null first whether or not it could be null.

06:50.840 --> 06:55.820
So it's just a helpful flag to have enabled, and we'll talk about it more as we build our application

06:55.820 --> 06:57.020
as we'll need to.

06:57.050 --> 06:59.060
Also, we've got this implicit usings.

06:59.060 --> 07:02.060
This is helpful to remove boilerplate in our code.

07:02.090 --> 07:06.410
When it comes to using statements, which we'll talk about as we come across them.

07:06.410 --> 07:11.630
So this is just our project file, and that's about as small as we can get it for our purposes right

07:11.630 --> 07:12.170
now.

07:12.200 --> 07:14.840
A few other things I'll mention just before we move forward.

07:14.840 --> 07:17.600
We have configuration files for our project.

07:17.780 --> 07:24.380
Our projects can receive configuration from these appsettings.json files or app settings dot JSON file.

07:24.500 --> 07:31.220
Now when we're running in development mode then it can read from app settings development JSON as well

07:31.220 --> 07:33.110
as Appsettings.json.

07:33.260 --> 07:39.530
But the more specific version, as in if we're in development and app settings development has the same

07:39.530 --> 07:45.590
value key value pair as Appsettings.json, then the more specific one is used.

07:45.980 --> 07:50.780
And if we're running in production mode, then this file is ignored.

07:50.780 --> 07:56.180
One small change I want to make in here, I'm going to change it from warning to information, just

07:56.180 --> 08:00.860
so that we get maximum information about what's going on as we're running our project.

08:00.870 --> 08:06.990
particularly useful for SQL type queries, so we can see the full query that's being presented to our

08:06.990 --> 08:08.370
database server.

08:08.670 --> 08:10.290
We've also got a controllers folder.

08:10.290 --> 08:15.540
We're going to have API controllers with API controller endpoints that are going to receive the HTTP

08:15.540 --> 08:18.150
requests and respond to them.

08:18.150 --> 08:22.800
We'll come back to these as we'll be creating some of these a bit later.

08:22.920 --> 08:28.230
Another thing I want to point out is inside this properties folder we've got a launch settings.json.

08:28.230 --> 08:34.230
And inside here this is what when we run dotnet run, this is what it looks for to see what it should

08:34.230 --> 08:34.980
start on.

08:34.980 --> 08:41.130
So we can see that the application URL in my case is localhost 5164.

08:41.130 --> 08:43.140
And it would be slightly different for yourself.

08:43.140 --> 08:45.930
I do actually want to change this.

08:45.960 --> 08:47.280
We don't need two profiles.

08:47.280 --> 08:50.250
We've got an HTTP profile and we've got an Https one.

08:50.700 --> 08:56.130
I'm actually going to remove the HTTP one and get rid of it in its entirety.

08:56.130 --> 08:58.590
So we're just going to be using the Https one.

08:59.070 --> 09:01.530
And we don't want to launch the browser.

09:01.530 --> 09:07.680
It's a web API project, and we're just going to start our project on Https.

09:07.680 --> 09:14.100
And I'm just going to change the port number to what was previously the standard default port for a

09:14.130 --> 09:19.590
dotnet project, and change it to https localhost 5001.

09:20.250 --> 09:22.050
Everything else can stay the same.

09:22.080 --> 09:28.140
Now if we stop and restart our project, we should see that change in place.

09:28.140 --> 09:30.630
And I'm just going to execute, not dotnet run.

09:30.630 --> 09:37.440
This time we're going to use dotnet watch because this comes with a file watcher and some basic hot

09:37.440 --> 09:42.690
reloading functionality which doesn't work particularly well, but we'll use it anyway.

09:42.870 --> 09:49.260
And what we should find is that our project is now started on https localhost 5001.

09:49.260 --> 09:55.410
And if we go across to our browser and we put that URL in, then of course we get the page cannot be

09:55.410 --> 09:58.890
found because nothing is listening on the root address.

09:58.890 --> 10:05.830
But if we go to Have the forecast, then we should find that we still get our weather forecast summaries

10:05.830 --> 10:09.430
and we can see that our connection is secured, so on and so forth.

10:09.430 --> 10:11.470
And if that's working for you, brilliant.

10:11.470 --> 10:13.000
Please move on to the next lesson.

10:13.000 --> 10:18.550
If you did have a security warning about not trusted certificates, then I'm just going to demonstrate

10:18.550 --> 10:20.440
what you need to do right now.

10:20.440 --> 10:22.030
So stop your application.

10:22.030 --> 10:35.200
And inside the terminal you just want to execute dotnet dev dash certs https and dash dash clean and

10:35.200 --> 10:36.730
execute this commands.

10:36.730 --> 10:41.950
And then change clean to trust and then execute that command.

10:41.950 --> 10:47.380
And that should enable your computer to trust the self-signed certificate that comes with dotnet.

10:47.380 --> 10:52.690
So if you did run into issues with trusted certificates then please run that command.

10:52.690 --> 10:54.100
But anyway that's our project.

10:54.100 --> 10:56.890
So just a very brief overview of what's contained inside here.

10:56.890 --> 10:58.180
And now we have this in place.

10:58.180 --> 11:01.930
We can move on to the next lesson and create our first entity.
