⭐ If you would like to buy me a coffee, well thank you very much that is mega kind! : https://www.buymeacoffee.com/honeyvig Hire a web Developer and Designer to upgrade and boost your online presence with cutting edge Technologies

Friday, November 7, 2025

.NET 5 REST API Tutorial: 01 Getting Started

 This is the first part of my .NET 5 REST API Tutorial. You will learn: • The scenario to be used across the tutorial • How to create a .NET 5 Web API project from scratch • How to use VS Code for building and debugging the project • How to trust the development certificate • How to use Swagger UI to interact with the API 

0:01

a rest API allows your app or system to

0:03

expose its functionality to multiple

0:05

types of clients both inside and outside

0:08

of your network including clients across

0:10

the internet this is what you would use

0:12

if you wanted to write a program to

0:13

collect data from say Twitter Yahoo

0:16

finance or even NASA if you're looking

0:19

into building your own res API and

0:21

you're considering the net platform for

0:23

it please stay with me as I show you how

0:25

to do this end to endend using the

0:27

latest Innovations provided by Net 5

0:30

I hope you enjoy

0:33

it in the first part of this tutorial

0:35

you're going to learn the scenario to be

0:38

used across a tutorial how to create a

0:41

net 5 web API project from

0:44

scratch how to use Visual Studio code

0:47

for building and debugging the project

0:49

we're going to work

0:51

on how to trust the development

0:53

certificate installed by Net 5 and

0:55

that's going to be needed for https

0:58

access and how to use Swagger UI to

1:01

interact with the

1:04

API to follow the tutorial you're going

1:07

to need a few things including the net 5

1:12

SDK Visual Studio

1:15

code and some basic understanding of the

1:17

c c

1:21

language now let's talk about the

1:23

scenario that we're going to be using uh

1:25

as a domain for this tutorial so let's

1:28

imagine that we have some sort of a

1:30

catalog system uh and that we have a

1:33

bunch of items available in it so in

1:35

this sense uh like I am uh video video

1:38

gamer so I like to think of these items

1:40

as items that I would use within a video

1:42

game so items like potions uh sorts

1:46

Shields and stuff like that right so

1:49

that's a system that we have in place it

1:50

has a catalog and uh of course we're

1:53

going to have users that are going to be

1:57

would like to manage this catalog uh via

2:00

their their browser right they have a

2:02

browser they want to manage this catalog

2:04

of items

2:05

somehow so and things that they would

2:08

they would like to do is well uh how are

2:10

we going to create items in the catalog

2:13

H how can we retrieve uh the the list of

2:16

items currently available in this

2:18

catalog how can we update properties uh

2:21

of of the items and how can we delete

2:24

items in this catalog so as it is today

2:27

h we do have the catalog available but

2:30

we lack uh or we don't have uh a way to

2:34

expose this catalog to the Internet so

2:36

that people can just go ahead and manage

2:38

it from the browser so that's where

2:41

we're going to introduce a res API for

2:43

this catalog and uh during this tutorial

2:47

we're going to see how to build this R

2:48

API from scratch using Net

2:52

5 so here we are in Visual Studio code

2:55

and the first thing we're going to do is

2:57

open a brand new

2:58

terminal and in this terminal we're just

3:01

going to switch to the directory uh

3:03

where we're going to create our project

3:05

and to create a project we're going to

3:07

be using the net

3:08

CLI uh so to do that we're just going to

3:11

say dot net new and the type of project

3:14

that we want that we want to create for

3:15

a rest API that's going to be web

3:18

API and the name of the project is going

3:21

to be catalog hit

3:23

enter and that creates generates all the

3:26

files based on the web API template so

3:29

now I'm going to open that folder that

3:31

got generated

3:35

catalog and uh as you open as usual as

3:39

you open a net project in Vis Studio

3:41

code it will prompt you to add a few

3:43

additional files for building and

3:44

debugging the project so I'm going to

3:45

say yes and those files get generated

3:48

under that vs code so on the left side

3:50

you can actually see all the generated

3:52

files and let's take a quick look at at

3:54

each of these

3:55

files as a quick lab around so the first

3:59

file we're going to look look at is the

4:00

cspr file and this file uh is used this

4:04

is called the project file and this is

4:06

used to declare how we're going to build

4:07

this project in this case we're saying

4:09

that we're going to be using the net web

4:11

SDK uh to build the project which

4:13

includes a bunch of tools and tasks uh

4:16

to specifically designed for web kind of

4:19

projects the next interesting thing will

4:21

be the target framework H monitor or

4:23

Target framework which in our case is

4:25

net five uh the target framework defines

4:28

uh the API are face or what kind of apis

4:31

are going to be available to your

4:33

project in this case net five is

4:35

perfectly good for us and the next thing

4:38

is going to be a bunch of nou packages

4:40

that we're not going to be diving into

4:42

right now but those are just

4:43

dependencies that we already acquir um

4:46

on this project close that and the next

4:49

file that I'm going to take a look is

4:50

just program CS program CS is what we

4:53

call the entry point of the application

4:55

and what this will do is just uh pretty

4:57

much uh raise or stand up the host that

5:01

I mean the yeah the process that's going

5:03

to become the host of our progam and it

5:05

also declares a bunch of defaults and it

5:08

also sets up what it what we call the

5:11

start app uh startup class for our

5:13

project so let's actually go to Startup

5:16

and's see what's going on there uh

5:18

really the main things in startup are

5:21

that uh we have this property called

5:24

configuration uh that we receive as part

5:26

of the startup Constructor and this you

5:29

can use anytime you need to read some

5:31

configuration information from multiple

5:33

sources like from environment variables

5:36

or files different kinds of files or a

5:39

bunch of other places configuration that

5:41

you don't want to hardcode into your

5:43

service the next interesting method is

5:45

configure services and this is the the

5:47

place where you would uh register all

5:51

the services that you're going to be

5:52

using across your application we'll talk

5:54

about this later on in this video and

5:57

the last interesting piece is the

5:59

configure

6:00

method this is what we configure what we

6:03

call the

6:05

ER the pipeline right the request

6:08

Pipeline on asp.net and so this just

6:11

defines a bunch of what we call middle

6:13

Wares which are a additional components

6:16

that will execute uh before uh your um

6:22

before your your controller or your your

6:24

code actually executes so each of the

6:26

each of these can execute a uh as a

6:30

request comes into H into the aset

6:33

process and from there all the way into

6:36

when your code executes uh but we're not

6:39

going to be exploring this part in this

6:41

video um a couple of other things are uh

6:44

so we have a weather forecast so this is

6:47

a a model that gets autogenerated for

6:50

the sample application here just have a

6:52

few very simple properties here and uh

6:56

alongside this uh this model there's a

6:59

controller now the controller in asp.net

7:03

is just uh pretty much the class that

7:06

handles the routes uh yeah pretty much

7:10

the routes that uh your service exposes

7:13

right but we're not going to be using

7:15

this in this video so let's not dive too

7:17

much into

7:18

that a few other files upsetting Json

7:21

this is where you can declare

7:22

configuration uh that's going to be that

7:24

you don't want to use hardcode into uh

7:27

your into your your program your your

7:29

source code uh so and right now it just

7:32

has some configuration for login and the

7:34

host that are allowed in the app there's

7:36

also a variant of appsettings Json which

7:38

is upsetting development Json uh so

7:41

developing I mean the fact that it that

7:43

says does say up settings. development.

7:46

Json means that when we're running in

7:48

development environment uh these

7:51

settings will take precedence on top of

7:53

the app settings Json so you could have

7:54

a bunch of these app settings files for

7:56

each of your uh environment like

7:58

production test integration all all of

8:00

these environments um now if we talk

8:03

about environments uh may be a good time

8:05

to take a look at the file generated

8:07

that vs code uh which are task Json and

8:10

launch launch Json task Json is just a

8:13

file that declares the tasks uh this is

8:16

a a key concept on Vis Studio code you

8:18

can declare tasks and and a task is can

8:21

be a bunch of things and in our case the

8:23

most interesting task is the fact that

8:25

we want to just run the net build

8:27

command so net build which is going to

8:29

be used for building our

8:31

code and uh in terms of launch ad Json

8:34

this is the file that controls what is

8:36

going to be launch or executed and when

8:38

we do like an F5 or when we start

8:40

debugging the code in this case it's

8:41

already pointing to the right dll for

8:43

start to start

8:45

debugging lastly uh we also have launch

8:49

settings at Json and really the only

8:51

interesting part here that that I'd like

8:53

to you to take a look is the application

8:54

URL uh here we are defining the URLs uh

8:58

URLs in for our application uh in which

9:02

case in this case we're saying we're

9:03

going to be serving our our our server

9:06

uh in a local host 5,000 and for the

9:09

htps version we're reserving it which is

9:12

going to be the default version uh is

9:14

going to be

9:15

50001 we're also declaring the actual H

9:19

cord environment environment variable uh

9:22

and we setting it as development and

9:25

so uh Al to in Vis Studio code this is

9:28

not the actual one that's been honored

9:30

the one that will is going to be honored

9:31

is in lunch Json uh as you see over here

9:34

this is the one that takes presentent if

9:36

stud cover so so that's good um what we

9:41

want to do now is actually to test this

9:44

this uh project just to make sure that

9:46

everything is running as expected so

9:47

what I'm going to do is I'm going to

9:49

switch to the theog hub over here let me

9:52

expand this a little bit and so what I'm

9:55

going to do is just click and start

9:57

debuging let's see what happens

10:05

all right so browser pops up and then if

10:09

you're getting this page here what it

10:11

means uh it's Prett normal it means that

10:14

uh we don't have uh the self-signed

10:16

certificate that comes with net it has

10:18

not been H trusted right and so and

10:22

there's a very simple way to go around

10:25

decision and to properly trust the

10:26

certificate that comes with net so let

10:29

me actually just close this and stop and

10:34

let's switch back to the terminal the

10:37

actually let me open a new terminal here

10:39

yeah there it

10:41

is so in order to trust the certificate

10:45

that comes bundled with the net SDK H

10:48

what you have to do is just

10:49

type net

10:52

defert

10:55

https

10:57

trust when you run this is you're going

10:59

to get this popup uh asking you to

11:02

confirm that you actually want to trust

11:04

thater I'm going to say yes and then uh

11:08

that pretty much should do it so I'm

11:10

going to uh run it

11:13

again let's see what we

11:19

get all right so yeah so we still are

11:22

not getting pretty much anything here

11:24

but we are not seeing any trust issue

11:27

anymore now if you actually want to see

11:29

something meaningful here although it

11:31

doesn't matter too much for us at this

11:32

point what you can do is just go to

11:34

Swagger and then you're going to get

11:36

this this nice UI so this is what we

11:39

call Swagger or also open API

11:41

specification uh so this is a component

11:44

that's is bundled now with Net 5 uh so

11:47

you don't have to do anything to make it

11:49

available as you saw we did nothing uh

11:51

so it's just there uh in the/ Swagger

11:54

URL and what this does is allows you to

11:57

uh easily uh describe all the operations

12:01

all the actions all the routes that are

12:03

available in your API and allows you to

12:05

also interact with them easily so for

12:08

example if I just go to the get route

12:10

and I click H try it out click execute

12:14

it will go ahead and run it and we can

12:16

see already some results for that

12:22

route and then one thing that that I

12:24

like to do uh as I work on these

12:25

projects is to uh really not uh open a

12:28

browser every single time that that I

12:30

run the project uh so let let's actually

12:34

switch a little bit the behavior of BS

12:36

code so that we'll just keep this window

12:38

open and then uh anytime we just hit a

12:41

five or hit run uh it will not open more

12:43

windows so to do that I'm going to stop

12:47

here and we can go to launch the Json

12:51

and close

12:54

this the only thing that you have to do

12:56

is just remove the server ready action

12:58

section here

13:00

here and that's pretty much it if I run

13:04

again and by the way you can do this by

13:06

just pressing a

13:08

F5 which I'll do right now

13:13

F5 that starts the host as you can see

13:16

but we did not it did not open any more

13:18

windows which is fine because we have

13:22

the browser ready to go right

13:24

there then the last thing that I like to

13:26

do in terms of a project setup is to

13:29

simplify how we build our project in

13:31

Visual Studio code so for that let me

13:33

minimize this and go back to visual

13:36

studio code stop this close that what

13:38

I'm going to do is to go to Tas adjacent

13:42

and the only thing that I'm going to do

13:44

is uh to add this little section here

13:47

under the build task uh which is uh just

13:52

the what we call the group which is kind

13:54

buil and it says it's Default true what

13:56

this does is that allows us to easily

13:59

build a project so now I can go to let

14:02

me just save this saved I can go to uh

14:06

terminal room build tasks and it

14:09

immediately builds um without that we it

14:12

will just have pop up yet another menu

14:15

to do the build I can also now do

14:16

control shift B and it will do the same

14:19

thing so yes that just spits up our

14:21

build

14:23

situation so that's it for this video in

14:25

the next one we're going to be

14:26

introducing the domain entity which is

14:29

item uh for this um for this service and

14:33

also we'll introducing our repository

14:35

class which is the one that's going to

14:36

be in charge of both getting and uh

14:40

persisting um the the items are going to

14:42

be created across the service so yeah I

14:45

hope this was useful and uh yeah if you

14:48

like it uh please consider hitting the

14:49

like button and don't forget to

14:51

subscribe uh so that you know when I

14:53

publish any new videos uh thanks a lot

14:55

for watching and I'll see you next time

 

No comments:

Post a Comment