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
a rest API allows your app or system to
expose its functionality to multiple
types of clients both inside and outside
of your network including clients across
the internet this is what you would use
if you wanted to write a program to
collect data from say Twitter Yahoo
finance or even NASA if you're looking
into building your own res API and
you're considering the net platform for
it please stay with me as I show you how
to do this end to endend using the
latest Innovations provided by Net 5
I hope you enjoy
it in the first part of this tutorial
you're going to learn the scenario to be
used across a tutorial how to create a
net 5 web API project from
scratch how to use Visual Studio code
for building and debugging the project
we're going to work
on how to trust the development
certificate installed by Net 5 and
that's going to be needed for https
access and how to use Swagger UI to
interact with the
API to follow the tutorial you're going
to need a few things including the net 5
SDK Visual Studio
code and some basic understanding of the
c c
language now let's talk about the
scenario that we're going to be using uh
as a domain for this tutorial so let's
imagine that we have some sort of a
catalog system uh and that we have a
bunch of items available in it so in
this sense uh like I am uh video video
gamer so I like to think of these items
as items that I would use within a video
game so items like potions uh sorts
Shields and stuff like that right so
that's a system that we have in place it
has a catalog and uh of course we're
going to have users that are going to be
would like to manage this catalog uh via
their their browser right they have a
browser they want to manage this catalog
of items
somehow so and things that they would
they would like to do is well uh how are
we going to create items in the catalog
H how can we retrieve uh the the list of
items currently available in this
catalog how can we update properties uh
of of the items and how can we delete
items in this catalog so as it is today
h we do have the catalog available but
we lack uh or we don't have uh a way to
expose this catalog to the Internet so
that people can just go ahead and manage
it from the browser so that's where
we're going to introduce a res API for
this catalog and uh during this tutorial
we're going to see how to build this R
API from scratch using Net
5 so here we are in Visual Studio code
and the first thing we're going to do is
open a brand new
terminal and in this terminal we're just
going to switch to the directory uh
where we're going to create our project
and to create a project we're going to
be using the net
CLI uh so to do that we're just going to
say dot net new and the type of project
that we want that we want to create for
a rest API that's going to be web
API and the name of the project is going
to be catalog hit
enter and that creates generates all the
files based on the web API template so
now I'm going to open that folder that
got generated
catalog and uh as you open as usual as
you open a net project in Vis Studio
code it will prompt you to add a few
additional files for building and
debugging the project so I'm going to
say yes and those files get generated
under that vs code so on the left side
you can actually see all the generated
files and let's take a quick look at at
each of these
files as a quick lab around so the first
file we're going to look look at is the
cspr file and this file uh is used this
is called the project file and this is
used to declare how we're going to build
this project in this case we're saying
that we're going to be using the net web
SDK uh to build the project which
includes a bunch of tools and tasks uh
to specifically designed for web kind of
projects the next interesting thing will
be the target framework H monitor or
Target framework which in our case is
net five uh the target framework defines
uh the API are face or what kind of apis
are going to be available to your
project in this case net five is
perfectly good for us and the next thing
is going to be a bunch of nou packages
that we're not going to be diving into
right now but those are just
dependencies that we already acquir um
on this project close that and the next
file that I'm going to take a look is
just program CS program CS is what we
call the entry point of the application
and what this will do is just uh pretty
much uh raise or stand up the host that
I mean the yeah the process that's going
to become the host of our progam and it
also declares a bunch of defaults and it
also sets up what it what we call the
start app uh startup class for our
project so let's actually go to Startup
and's see what's going on there uh
really the main things in startup are
that uh we have this property called
configuration uh that we receive as part
of the startup Constructor and this you
can use anytime you need to read some
configuration information from multiple
sources like from environment variables
or files different kinds of files or a
bunch of other places configuration that
you don't want to hardcode into your
service the next interesting method is
configure services and this is the the
place where you would uh register all
the services that you're going to be
using across your application we'll talk
about this later on in this video and
the last interesting piece is the
configure
method this is what we configure what we
call the
ER the pipeline right the request
Pipeline on asp.net and so this just
defines a bunch of what we call middle
Wares which are a additional components
that will execute uh before uh your um
before your your controller or your your
code actually executes so each of the
each of these can execute a uh as a
request comes into H into the aset
process and from there all the way into
when your code executes uh but we're not
going to be exploring this part in this
video um a couple of other things are uh
so we have a weather forecast so this is
a a model that gets autogenerated for
the sample application here just have a
few very simple properties here and uh
alongside this uh this model there's a
controller now the controller in asp.net
is just uh pretty much the class that
handles the routes uh yeah pretty much
the routes that uh your service exposes
right but we're not going to be using
this in this video so let's not dive too
much into
that a few other files upsetting Json
this is where you can declare
configuration uh that's going to be that
you don't want to use hardcode into uh
your into your your program your your
source code uh so and right now it just
has some configuration for login and the
host that are allowed in the app there's
also a variant of appsettings Json which
is upsetting development Json uh so
developing I mean the fact that it that
says does say up settings. development.
Json means that when we're running in
development environment uh these
settings will take precedence on top of
the app settings Json so you could have
a bunch of these app settings files for
each of your uh environment like
production test integration all all of
these environments um now if we talk
about environments uh may be a good time
to take a look at the file generated
that vs code uh which are task Json and
launch launch Json task Json is just a
file that declares the tasks uh this is
a a key concept on Vis Studio code you
can declare tasks and and a task is can
be a bunch of things and in our case the
most interesting task is the fact that
we want to just run the net build
command so net build which is going to
be used for building our
code and uh in terms of launch ad Json
this is the file that controls what is
going to be launch or executed and when
we do like an F5 or when we start
debugging the code in this case it's
already pointing to the right dll for
start to start
debugging lastly uh we also have launch
settings at Json and really the only
interesting part here that that I'd like
to you to take a look is the application
URL uh here we are defining the URLs uh
URLs in for our application uh in which
case in this case we're saying we're
going to be serving our our our server
uh in a local host 5,000 and for the
htps version we're reserving it which is
going to be the default version uh is
going to be
50001 we're also declaring the actual H
cord environment environment variable uh
and we setting it as development and
so uh Al to in Vis Studio code this is
not the actual one that's been honored
the one that will is going to be honored
is in lunch Json uh as you see over here
this is the one that takes presentent if
stud cover so so that's good um what we
want to do now is actually to test this
this uh project just to make sure that
everything is running as expected so
what I'm going to do is I'm going to
switch to the theog hub over here let me
expand this a little bit and so what I'm
going to do is just click and start
debuging let's see what happens
all right so browser pops up and then if
you're getting this page here what it
means uh it's Prett normal it means that
uh we don't have uh the self-signed
certificate that comes with net it has
not been H trusted right and so and
there's a very simple way to go around
decision and to properly trust the
certificate that comes with net so let
me actually just close this and stop and
let's switch back to the terminal the
actually let me open a new terminal here
yeah there it
is so in order to trust the certificate
that comes bundled with the net SDK H
what you have to do is just
type net
defert
https
trust when you run this is you're going
to get this popup uh asking you to
confirm that you actually want to trust
thater I'm going to say yes and then uh
that pretty much should do it so I'm
going to uh run it
again let's see what we
get all right so yeah so we still are
not getting pretty much anything here
but we are not seeing any trust issue
anymore now if you actually want to see
something meaningful here although it
doesn't matter too much for us at this
point what you can do is just go to
Swagger and then you're going to get
this this nice UI so this is what we
call Swagger or also open API
specification uh so this is a component
that's is bundled now with Net 5 uh so
you don't have to do anything to make it
available as you saw we did nothing uh
so it's just there uh in the/ Swagger
URL and what this does is allows you to
uh easily uh describe all the operations
all the actions all the routes that are
available in your API and allows you to
also interact with them easily so for
example if I just go to the get route
and I click H try it out click execute
it will go ahead and run it and we can
see already some results for that
route and then one thing that that I
like to do uh as I work on these
projects is to uh really not uh open a
browser every single time that that I
run the project uh so let let's actually
switch a little bit the behavior of BS
code so that we'll just keep this window
open and then uh anytime we just hit a
five or hit run uh it will not open more
windows so to do that I'm going to stop
here and we can go to launch the Json
and close
this the only thing that you have to do
is just remove the server ready action
section here
here and that's pretty much it if I run
again and by the way you can do this by
just pressing a
F5 which I'll do right now
F5 that starts the host as you can see
but we did not it did not open any more
windows which is fine because we have
the browser ready to go right
there then the last thing that I like to
do in terms of a project setup is to
simplify how we build our project in
Visual Studio code so for that let me
minimize this and go back to visual
studio code stop this close that what
I'm going to do is to go to Tas adjacent
and the only thing that I'm going to do
is uh to add this little section here
under the build task uh which is uh just
the what we call the group which is kind
buil and it says it's Default true what
this does is that allows us to easily
build a project so now I can go to let
me just save this saved I can go to uh
terminal room build tasks and it
immediately builds um without that we it
will just have pop up yet another menu
to do the build I can also now do
control shift B and it will do the same
thing so yes that just spits up our
build
situation so that's it for this video in
the next one we're going to be
introducing the domain entity which is
item uh for this um for this service and
also we'll introducing our repository
class which is the one that's going to
be in charge of both getting and uh
persisting um the the items are going to
be created across the service so yeah I
hope this was useful and uh yeah if you
like it uh please consider hitting the
like button and don't forget to
subscribe uh so that you know when I
publish any new videos uh thanks a lot
for watching and I'll see you next time
No comments:
Post a Comment