Asp.Net Core Web API Client/Server Application | Visual Studio 2019
Summary
TLDRThis tutorial video guides viewers through creating ASP.NET Core Web API applications using Visual Studio 2019. It begins with an introduction to Web API and its use in building HTTP services accessible by various clients. The presenter demonstrates setting up a Web API server project, removing default configurations, and creating a simple GET method in a controller. A console application is then added as a client to interact with the server. The process includes configuring the client to make HTTP requests and handle responses. The video concludes with running both projects to display the data retrieved from the server, encouraging viewers to explore and build upon the concepts introduced.
Takeaways
- 😀 The video is a tutorial on creating ASP.NET Core Web API for both client and server applications using Visual Studio 2019.
- 🔍 The presenter explains that a Web API is an application programming interface for a web server or web browser.
- 🛠 ASP.NET Core Web API is a framework for building HTTP services accessible by clients such as browsers and mobile devices.
- 📚 The video demonstrates how to create a new project in Visual Studio 2019, specifically an ASP.NET Core Web API project named 'Web API Server'.
- 🚫 The tutorial opts not to configure the application for HTTPS and disables Open API support for simplicity.
- 📝 The script guides viewers through removing the default 'Weather Forecast' controller and creating a new 'Values' controller.
- 🔄 The presenter shows how to adjust the launch settings to point to the 'Values' controller instead of the default endpoint.
- 💻 A new Console App project is added to serve as the Web API client, named 'Web API Client'.
- 🔄 The video explains setting up multiple startup projects in Visual Studio to run both the server and client simultaneously.
- 📝 In the 'Values' controller, a simple GET method is created to return an array of strings as a response.
- 🔗 The client application uses HttpClient to make an asynchronous GET request to the server's URI and handles the response.
- 📡 The video concludes with running the projects and displaying the data retrieved from the server in the client console.
Q & A
What is the main topic of the video?
-The main topic of the video is about creating and understanding ASP.NET Core Web API for both client and server applications using Visual Studio 2019.
What is a Web API?
-A Web API is an application programming interface for a web server or a web browser, allowing access to services with HTTP requests from various clients like browsers and mobile devices.
What is ASP.NET Core Web API?
-ASP.NET Core Web API is a framework for building HTTP services that can be accessed by any client, making it an ideal platform for creating RESTful applications on the .NET platform.
Why is Visual Studio 2019 used in the video?
-Visual Studio 2019 is used as it is a powerful IDE that supports the development of ASP.NET Core applications, including Web APIs.
What is the first step in creating a Web API project in the video?
-The first step is to create a new project in Visual Studio 2019 and select ASP.NET Core Web API as the project type.
Why is the 'Weather Forecast' sample removed in the video?
-The 'Weather Forecast' sample is removed to create a clean slate for demonstrating the creation of a custom Web API without the default sample data.
What does the video suggest to do with 'Open API support'?
-The video suggests unchecking the 'Enable Open API support' option, as it is not necessary for the demonstration in the video.
How is the Values controller used in the video?
-The Values controller is used to create a simple GET method that returns a hardcoded array of strings, demonstrating a basic API endpoint.
What is the purpose of creating a Console App project in the video?
-A Console App project is created to act as a client that will consume the Web API, showcasing how to make HTTP requests to the server.
How does the video handle the server URI in the client application?
-The server URI is hardcoded in the client application's code after starting the server, ensuring the client knows where to send its requests.
What is the significance of setting the startup projects in Visual Studio?
-Setting multiple startup projects allows both the Web API server and the client application to start simultaneously, which is useful for testing and development.
How does the video demonstrate handling responses from the Web API?
-The video demonstrates checking if the response has a successful status code and then reading the content as a string. It also shows basic error handling by printing the response error code if the request fails.
What is the final step shown in the video?
-The final step is running both the Web API server and the client application to see the data being displayed in the client's console, demonstrating a successful API call.
Outlines
🚀 Introduction to ASP.NET Core Web API
This paragraph introduces the topic of the video, which is about creating ASP.NET Core Web API for both client and server applications using Visual Studio 2019. The speaker encourages viewers to subscribe to the channel and provides a brief explanation of what a Web API is, highlighting its role as an application programming interface for web servers or browsers. ASP.NET Core Web API is described as a framework for building HTTP services accessible by various clients, including browsers and mobile devices, and is ideal for creating RESTful applications.
🛠 Setting Up the Web API Server Project
The speaker demonstrates the process of setting up a new ASP.NET Core Web API project in Visual Studio 2019. They guide viewers through creating a blank solution, adding a new project, and selecting the correct version of the .NET framework. The video shows how to configure the project by removing unnecessary features like HTTPS configuration and disabling Open API support. The speaker also explains how to remove the default Weather Forecast controller and create a new Values controller, adjusting the launch settings to reflect these changes.
🔧 Creating a Simple GET Method in Values Controller
In this paragraph, the focus is on implementing a simple GET method within the Values controller. The speaker creates a method named 'Gets' that returns a new instance of a string array containing some sample data like 'mango', 'banana', and 'apple'. They also mention the importance of removing the '/api' prefix from the controller's URI to avoid conflicts. The speaker emphasizes the simplicity of the task, aiming to keep the demonstration straightforward for the viewers.
🔄 Developing the Web API Client Console Application
The speaker proceeds to develop a console application that will act as a client for the Web API server. They guide the viewers through creating a new C# console application project and setting it up to run alongside the Web API server. The speaker then demonstrates how to use the HttpClient class to make an asynchronous GET request to the server's URI. They explain the process of awaiting the response, checking for a successful status code, and then reading the content of the response to display it in the console. The video concludes with a demonstration of running both the server and client applications simultaneously and displaying the fetched data.
Mindmap
Keywords
💡ASP.NET Core
💡Web API
💡Visual Studio 2019
💡RESTful
💡Controller
💡HTTP GET method
💡Client
💡HTTP Client
💡URI
💡Startup Project
💡Open API Support
Highlights
Introduction to ASP.NET Core and Web API for both client and server applications using Visual Studio 2019.
Definition of Web API as an application programming interface for web servers or browsers.
Explanation of ASP.NET Core Web API as a framework for building HTTP services accessible by various clients.
Demonstration of creating a new ASP.NET Core Web API project in Visual Studio 2019.
Instructions on removing the default Weather Forecast example from the Web API project.
Guidance on adding a new controller to the Web API project.
Details on configuring the launch settings for the Web API project.
Process of creating a simple GET method within the Values Controller.
Introduction of creating a Console Application project for the Web API client.
Setting up multiple startup projects in Visual Studio to run both server and client simultaneously.
Implementation of an async method to handle HTTP GET requests in the client application.
Use of HttpClient to make requests to the Web API server from the client application.
Explanation on handling the response from the Web API server in the client application.
Demonstration of checking the success of the HTTP response and processing the data.
Displaying the retrieved data from the Web API server in the client application console.
Encouragement for viewers to subscribe to the channel for more upcoming videos.
Invitation for viewers to explore and build upon the concepts learned in the video.
Transcripts
hello everyone
welcome back once again in this video
we will look at asp.net core
web api both client
and server applications
using the visual studio 2019
but before we get started please make
sure you subscribe to the channel if you
haven't
so let's get to it then so what is
really a web
api so web api is just an
application programming interface for
either a web server
or their web browser so what is also
a web asp.net call web api
so that is
a framework for building http services
that can access by
that can be accessed by any client
including browsers
mobile devices etc
it is also an ideal platform for
building restful applications
on the dot net call so enough of all of
these so let's get to it then so as you
can see here i've got visual studio 2019
update so
we go straight into it then i actually
have a blank solution so i'm just going
to go by adding a new
project so if you don't have this then
obviously you just go
you just start by actually adding a new
project
then you can get to the solution side so
i'm just going to add a web api
asp.net call with api right
so we're just gonna click nest so here
we're just gonna name it
um web
api server
right so i'm just gonna click nest and
here we can select
whatever version of dot net framework to
target so we're not gonna look too much
into that
so obviously we're not going to actually
configure the application for https as
it's not necessary for this video and
also we're just going to remove the
enable open api support as well
so we're just going to uncheck this bit
i saw so in this
obviously we click on the create
so now we've actually got
a sample template created for us so what
we do is we're just going to remove this
bit here
um the weather forecast
and then we come inside the controller
that we remove the console
a bit as well so please follow this
part very carefully so once we we
actually remove
that yeah what we do next is we come
inside their
dependencies right so about before that
yeah
we right click our controller and then
we add a new item
so i'm just gonna select airspeed.net
call so what we do is we're going to add
an
empty api controller
so here as you can see we've got an
empty api
controller which is values controller
right
so once we've added the controller we've
got values controller so we actually
expand the properties then the launch
settings
so here as you can see we've got um the
launch url
to be weather forecast so we're going to
change this bit and
we've got the values controller so we're
just going to put values here instead
right so that's all we're going to do
for this bit we're just going to save
the file
and we can actually close it right so
the next bit obviously we're just going
to add a new
one more project and this project will
be
obviously a console app so it's a c
sharp console app right
and this project will be obviously
let's say web api
client
right so we add this as well we
click on the create and we should get
a console app created for us yeah so
as we can see here now the project is
all set to run
only the web api server so
whilst we're here we're just going to
right click on the solution
and then we come to the set startup
project
so as you can see i will click on the
multiple star project here
and then we we actually put both of them
on the start so that we can get both of
them started at the same time
so here we put start i saw we
apply the changes so as you can see the
changes
has been actually reflected here so
we've got multiple startup projects here
yeah so now what we do is we come inside
the values controller we're just going
to put nothing fancy for here
so we're just going to make a very
simple get method
so it's http get yeah
so we're just going to call this um
public
um string we're just gonna make some
string
array and what we call it is gets
so here what we do is we only are going
to return
a new instance of a string array
so we're just going to put some date as
in some day i
string this inside here so the first day
obviously i'm just going to put mango
the nest
i mean this is entirely up to you so you
can put whatever you want
but for this video i'm just gonna make
it as simple as possible
so we're just gonna put banana
and we're gonna pop her
and one more we're just gonna pour one
more
uh but um apple
just a moment please okay so
i mean i've actually got the comment
inside so i'm just going to pull it out
here
then then one
should go away so this is all we've
actually got there the get method so
what we do is we come inside
the client to web api client we open the
program
so here so right after the console.
right line here what we do is just gonna
put another another console.rt line here
so what we do is we're going to start by
using
so we're just going to put first
console the right line so here
we're just going to say press enter key
to continue
so what we do is we're just going to put
another console.red line here
and then what we do is we use the using
keyword so we'll be using
we're going to use http
client which will be clark clients
this will be according to the new
instance of our http client right
so here what we do is we actually
reference it
so we're going to use the the
system.net.http.http client
yeah so we're going to use the same for
this as well
system.html
yeah so that's what we're going to be
using now so what we do is we just put
inside a bracket so what we do is we
initialize a variable called
response this will be equal to so this
method what we do
is we're going to make the the main
method
async task
so here we're going to reference
system.task so
inside the response we're going to we
can await
yeah so we could say client
dot
get async
so we can put our uri
inside here so what we do is i'm just
going to make
the server to start up
just to start up yeah i'm going to set
it to start up
so you'll be just a server that will run
so we can get the uri
so actually before you can run with the
runner there's one more thing that
i need to show you
so we come inside the view values
controller so
basically here if you've got the slash
api just remove
that bit from the controller
uri yes so we stop it from running
then we come inside uh the programs are
cs code
so inside here we can paste the uri that
we just copied
inside here yeah
so what we can do is we can say
response dot ensure
success code
and then we can say if
response does is
success to success status code
that means we know that there is
successful the response is successful so
this is what we would do
we going to initialize a string variable
we know
it's a string array but we just we could
convert it to a string gallery but we're
just going to leave that for this video
so we could say
await response
dot content dot
reader string async
yeah so the next bit is obviously we
could say
console or rail line then we can put a
message inside the console
yeah so after that what we could just
do is else we put the else statement
here
so we could say console.rightline
a response
oh we could just leave it here
something like this you can just write
something like this so
response error code
will be so we can put the response code
here so if we say
response dot
status code yeah so that's what we're
going to leave for here for now
so as you can see it's very simple stuff
so what we do is we actually turn it
straight back to running the
the multiple projects so initialize
startup projects
so we've got them started already so
apply changes
and then we run it to see what actually
happens
so now we're just gonna wait for the so
that one is actually loaded
so we're just gonna put this bit here
and
actually get our console client
so we could press enter key to enter so
as you can see we've got the data now to
be displayed
here now so we could actually convert it
to our rate
it's just displaying a string so but you
get you get
what is going on here now so what i'm
going to do is leave the video here for
now
so you can actually explore it further
and build on
what you've actually learned from here
so once again
if you haven't subscribed the channel
please make sure you do because i've got
lots of videos coming up
stay blessed
関連動画をさらに表示
Creating a Web API Project
How to run your own local Telegram Bot API server in Python [PTB v13]
Install and Configure DHCP Server in Windows Server 2019 Step By Step Guide
Complete CRUD Operation in Asp.Net C# With SQL Server Step by Step
O que é uma arquitetura de uma aplicação web?
Server Side Rendering with Vue.js 3
5.0 / 5 (0 votes)