00:00:01
hello everyone
00:00:03
welcome back once again in this video
00:00:06
we will look at asp.net core
00:00:10
web api both client
00:00:14
and server applications
00:00:18
using the visual studio 2019
00:00:21
but before we get started please make
00:00:23
sure you subscribe to the channel if you
00:00:25
haven't
00:00:26
so let's get to it then so what is
00:00:29
really a web
00:00:30
api so web api is just an
00:00:33
application programming interface for
00:00:36
either a web server
00:00:37
or their web browser so what is also
00:00:41
a web asp.net call web api
00:00:45
so that is
00:00:48
a framework for building http services
00:00:52
that can access by
00:00:55
that can be accessed by any client
00:00:58
including browsers
00:00:59
mobile devices etc
00:01:02
it is also an ideal platform for
00:01:05
building restful applications
00:01:07
on the dot net call so enough of all of
00:01:09
these so let's get to it then so as you
00:01:12
can see here i've got visual studio 2019
00:01:15
update so
00:01:16
we go straight into it then i actually
00:01:18
have a blank solution so i'm just going
00:01:20
to go by adding a new
00:01:22
project so if you don't have this then
00:01:25
obviously you just go
00:01:26
you just start by actually adding a new
00:01:28
project
00:01:30
then you can get to the solution side so
00:01:33
i'm just going to add a web api
00:01:35
asp.net call with api right
00:01:40
so we're just gonna click nest so here
00:01:42
we're just gonna name it
00:01:44
um web
00:01:49
api server
00:01:53
right so i'm just gonna click nest and
00:01:56
here we can select
00:01:58
whatever version of dot net framework to
00:02:00
target so we're not gonna look too much
00:02:02
into that
00:02:03
so obviously we're not going to actually
00:02:06
configure the application for https as
00:02:08
it's not necessary for this video and
00:02:11
also we're just going to remove the
00:02:13
enable open api support as well
00:02:16
so we're just going to uncheck this bit
00:02:18
i saw so in this
00:02:20
obviously we click on the create
00:02:24
so now we've actually got
00:02:28
a sample template created for us so what
00:02:30
we do is we're just going to remove this
00:02:32
bit here
00:02:33
um the weather forecast
00:02:37
and then we come inside the controller
00:02:40
that we remove the console
00:02:41
a bit as well so please follow this
00:02:45
part very carefully so once we we
00:02:47
actually remove
00:02:48
that yeah what we do next is we come
00:02:51
inside their
00:02:52
dependencies right so about before that
00:02:54
yeah
00:02:55
we right click our controller and then
00:02:58
we add a new item
00:03:04
so i'm just gonna select airspeed.net
00:03:06
call so what we do is we're going to add
00:03:09
an
00:03:10
empty api controller
00:03:14
so here as you can see we've got an
00:03:16
empty api
00:03:18
controller which is values controller
00:03:20
right
00:03:21
so once we've added the controller we've
00:03:23
got values controller so we actually
00:03:25
expand the properties then the launch
00:03:28
settings
00:03:30
so here as you can see we've got um the
00:03:33
launch url
00:03:34
to be weather forecast so we're going to
00:03:36
change this bit and
00:03:37
we've got the values controller so we're
00:03:39
just going to put values here instead
00:03:41
right so that's all we're going to do
00:03:44
for this bit we're just going to save
00:03:45
the file
00:03:46
and we can actually close it right so
00:03:49
the next bit obviously we're just going
00:03:50
to add a new
00:03:51
one more project and this project will
00:03:54
be
00:03:55
obviously a console app so it's a c
00:03:58
sharp console app right
00:04:01
and this project will be obviously
00:04:05
let's say web api
00:04:12
client
00:04:15
right so we add this as well we
00:04:19
click on the create and we should get
00:04:22
a console app created for us yeah so
00:04:25
as we can see here now the project is
00:04:27
all set to run
00:04:28
only the web api server so
00:04:32
whilst we're here we're just going to
00:04:33
right click on the solution
00:04:35
and then we come to the set startup
00:04:38
project
00:04:39
so as you can see i will click on the
00:04:41
multiple star project here
00:04:43
and then we we actually put both of them
00:04:46
on the start so that we can get both of
00:04:48
them started at the same time
00:04:50
so here we put start i saw we
00:04:54
apply the changes so as you can see the
00:04:57
changes
00:04:57
has been actually reflected here so
00:04:59
we've got multiple startup projects here
00:05:02
yeah so now what we do is we come inside
00:05:06
the values controller we're just going
00:05:08
to put nothing fancy for here
00:05:10
so we're just going to make a very
00:05:11
simple get method
00:05:13
so it's http get yeah
00:05:18
so we're just going to call this um
00:05:20
public
00:05:23
um string we're just gonna make some
00:05:25
string
00:05:26
array and what we call it is gets
00:05:32
so here what we do is we only are going
00:05:35
to return
00:05:37
a new instance of a string array
00:05:42
so we're just going to put some date as
00:05:44
in some day i
00:05:46
string this inside here so the first day
00:05:49
obviously i'm just going to put mango
00:05:52
the nest
00:05:55
i mean this is entirely up to you so you
00:05:57
can put whatever you want
00:05:59
but for this video i'm just gonna make
00:06:01
it as simple as possible
00:06:03
so we're just gonna put banana
00:06:06
and we're gonna pop her
00:06:11
and one more we're just gonna pour one
00:06:13
more
00:06:15
uh but um apple
00:06:28
just a moment please okay so
00:06:31
i mean i've actually got the comment
00:06:33
inside so i'm just going to pull it out
00:06:34
here
00:06:35
then then one
00:06:38
should go away so this is all we've
00:06:40
actually got there the get method so
00:06:42
what we do is we come inside
00:06:46
the client to web api client we open the
00:06:48
program
00:06:49
so here so right after the console.
00:06:54
right line here what we do is just gonna
00:06:55
put another another console.rt line here
00:07:02
so what we do is we're going to start by
00:07:05
using
00:07:08
so we're just going to put first
00:07:14
console the right line so here
00:07:19
we're just going to say press enter key
00:07:22
to continue
00:07:28
so what we do is we're just going to put
00:07:30
another console.red line here
00:07:40
and then what we do is we use the using
00:07:42
keyword so we'll be using
00:07:46
we're going to use http
00:07:53
client which will be clark clients
00:07:57
this will be according to the new
00:08:01
instance of our http client right
00:08:06
so here what we do is we actually
00:08:08
reference it
00:08:12
so we're going to use the the
00:08:16
system.net.http.http client
00:08:19
yeah so we're going to use the same for
00:08:20
this as well
00:08:24
system.html
00:08:26
yeah so that's what we're going to be
00:08:29
using now so what we do is we just put
00:08:32
inside a bracket so what we do is we
00:08:35
initialize a variable called
00:08:39
response this will be equal to so this
00:08:42
method what we do
00:08:43
is we're going to make the the main
00:08:45
method
00:08:47
async task
00:08:50
so here we're going to reference
00:08:55
system.task so
00:08:58
inside the response we're going to we
00:09:01
can await
00:09:02
yeah so we could say client
00:09:06
dot
00:09:09
get async
00:09:14
so we can put our uri
00:09:17
inside here so what we do is i'm just
00:09:19
going to make
00:09:20
the server to start up
00:09:24
just to start up yeah i'm going to set
00:09:27
it to start up
00:09:29
so you'll be just a server that will run
00:09:31
so we can get the uri
00:09:38
so actually before you can run with the
00:09:40
runner there's one more thing that
00:09:42
i need to show you
00:09:45
so we come inside the view values
00:09:47
controller so
00:09:49
basically here if you've got the slash
00:09:52
api just remove
00:09:53
that bit from the controller
00:10:16
uri yes so we stop it from running
00:10:21
then we come inside uh the programs are
00:10:24
cs code
00:10:25
so inside here we can paste the uri that
00:10:28
we just copied
00:10:30
inside here yeah
00:10:33
so what we can do is we can say
00:10:37
response dot ensure
00:10:40
success code
00:10:44
and then we can say if
00:10:48
response does is
00:10:52
success to success status code
00:10:56
that means we know that there is
00:10:59
successful the response is successful so
00:11:01
this is what we would do
00:11:03
we going to initialize a string variable
00:11:06
we know
00:11:07
it's a string array but we just we could
00:11:10
convert it to a string gallery but we're
00:11:11
just going to leave that for this video
00:11:13
so we could say
00:11:15
await response
00:11:19
dot content dot
00:11:22
reader string async
00:11:26
yeah so the next bit is obviously we
00:11:30
could say
00:11:31
console or rail line then we can put a
00:11:33
message inside the console
00:11:35
yeah so after that what we could just
00:11:39
do is else we put the else statement
00:11:41
here
00:11:44
so we could say console.rightline
00:11:50
a response
00:11:53
oh we could just leave it here
00:11:57
something like this you can just write
00:11:58
something like this so
00:12:02
response error code
00:12:06
will be so we can put the response code
00:12:09
here so if we say
00:12:11
response dot
00:12:15
status code yeah so that's what we're
00:12:19
going to leave for here for now
00:12:21
so as you can see it's very simple stuff
00:12:23
so what we do is we actually turn it
00:12:25
straight back to running the
00:12:26
the multiple projects so initialize
00:12:29
startup projects
00:12:30
so we've got them started already so
00:12:33
apply changes
00:12:34
and then we run it to see what actually
00:12:36
happens
00:12:39
so now we're just gonna wait for the so
00:12:41
that one is actually loaded
00:12:43
so we're just gonna put this bit here
00:12:45
and
00:12:46
actually get our console client
00:12:50
so we could press enter key to enter so
00:12:53
as you can see we've got the data now to
00:12:55
be displayed
00:12:57
here now so we could actually convert it
00:12:59
to our rate
00:13:00
it's just displaying a string so but you
00:13:02
get you get
00:13:03
what is going on here now so what i'm
00:13:06
going to do is leave the video here for
00:13:07
now
00:13:09
so you can actually explore it further
00:13:11
and build on
00:13:12
what you've actually learned from here
00:13:13
so once again
00:13:15
if you haven't subscribed the channel
00:13:16
please make sure you do because i've got
00:13:18
lots of videos coming up
00:13:24
stay blessed