Asp.Net Core Web API Client/Server Application | Visual Studio 2019

Hacked
31 Mar 202113:27

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

00:00

πŸš€ 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.

05:03

πŸ›  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.

10:17

πŸ”§ 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

ASP.NET Core is an open-source, cross-platform framework for building modern, cloud-based, Internet-connected applications. It is a key component of the .NET Core ecosystem and is used for developing web applications and APIs. In the video, ASP.NET Core is mentioned as the framework for creating a web API, which is a type of application programming interface specifically designed for web servers or web browsers.

πŸ’‘Web API

A Web API is a set of protocols and endpoints that allow different systems to communicate with each other over the web. It is used for building HTTP services that can be accessed by various clients, including browsers and mobile devices. In the video, the presenter discusses creating a Web API using ASP.NET Core, which is intended to be a RESTful service.

πŸ’‘Visual Studio 2019

Visual Studio 2019 is an integrated development environment (IDE) from Microsoft, used for developing applications across different platforms and languages. In the context of the video, it is the software used to create both the server and client applications for the web API, demonstrating its versatility for full-stack development.

πŸ’‘RESTful

RESTful refers to the Representational State Transfer architectural style for networked hypermedia applications. It relies on a stateless, client-server, cacheable communications protocol, and in web services, HTTP is commonly used. The video mentions building RESTful applications on the .NET Core, which implies creating APIs that follow REST principles for easy consumption by clients.

πŸ’‘Controller

In the context of ASP.NET Core, a controller is a class that handles incoming HTTP requests and returns HTTP responses. It is a central part of the MVC (Model-View-Controller) pattern used in web application development. The video script describes creating a 'ValuesController' as part of setting up the web API.

πŸ’‘HTTP GET method

The HTTP GET method is used to request data from a specified resource. It is one of the most common HTTP methods and is typically used to retrieve information. In the video, a GET method is added to the ValuesController to return an array of strings, demonstrating a simple way to serve data via the API.

πŸ’‘Client

In the context of web APIs, a client is any application or service that consumes the API to perform actions or retrieve data. The video script includes the creation of a console application that acts as a client to the web API, illustrating how a client might interact with the server.

πŸ’‘HTTP Client

The HTTP Client is a class in the System.Net.Http namespace that allows for sending HTTP requests and receiving HTTP responses. In the video, the presenter uses an HTTP Client to make a request to the web API from the client application, showcasing how to consume the API.

πŸ’‘URI

URI stands for Uniform Resource Identifier, which is a string of characters used to identify a name or a resource on the Internet. In the video, the presenter mentions setting up a URI for the web API client to make a request to the server, which is essential for locating and accessing the API endpoint.

πŸ’‘Startup Project

In Visual Studio, a startup project is the project that runs when you start the debugging or run process. The video script describes setting multiple startup projects, allowing both the web API server and the client application to start simultaneously, which is useful for testing and running both parts of the application at once.

πŸ’‘Open API Support

Open API, previously known as Swagger, is a specification for machine-readable interface files for RESTful APIs. It allows for the generation of interactive documentation and client SDKs. The video mentions disabling Open API support, which suggests that while it's a valuable tool for API development, it's not required for the basic demonstration being given.

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

play00:01

hello everyone

play00:03

welcome back once again in this video

play00:06

we will look at asp.net core

play00:10

web api both client

play00:14

and server applications

play00:18

using the visual studio 2019

play00:21

but before we get started please make

play00:23

sure you subscribe to the channel if you

play00:25

haven't

play00:26

so let's get to it then so what is

play00:29

really a web

play00:30

api so web api is just an

play00:33

application programming interface for

play00:36

either a web server

play00:37

or their web browser so what is also

play00:41

a web asp.net call web api

play00:45

so that is

play00:48

a framework for building http services

play00:52

that can access by

play00:55

that can be accessed by any client

play00:58

including browsers

play00:59

mobile devices etc

play01:02

it is also an ideal platform for

play01:05

building restful applications

play01:07

on the dot net call so enough of all of

play01:09

these so let's get to it then so as you

play01:12

can see here i've got visual studio 2019

play01:15

update so

play01:16

we go straight into it then i actually

play01:18

have a blank solution so i'm just going

play01:20

to go by adding a new

play01:22

project so if you don't have this then

play01:25

obviously you just go

play01:26

you just start by actually adding a new

play01:28

project

play01:30

then you can get to the solution side so

play01:33

i'm just going to add a web api

play01:35

asp.net call with api right

play01:40

so we're just gonna click nest so here

play01:42

we're just gonna name it

play01:44

um web

play01:49

api server

play01:53

right so i'm just gonna click nest and

play01:56

here we can select

play01:58

whatever version of dot net framework to

play02:00

target so we're not gonna look too much

play02:02

into that

play02:03

so obviously we're not going to actually

play02:06

configure the application for https as

play02:08

it's not necessary for this video and

play02:11

also we're just going to remove the

play02:13

enable open api support as well

play02:16

so we're just going to uncheck this bit

play02:18

i saw so in this

play02:20

obviously we click on the create

play02:24

so now we've actually got

play02:28

a sample template created for us so what

play02:30

we do is we're just going to remove this

play02:32

bit here

play02:33

um the weather forecast

play02:37

and then we come inside the controller

play02:40

that we remove the console

play02:41

a bit as well so please follow this

play02:45

part very carefully so once we we

play02:47

actually remove

play02:48

that yeah what we do next is we come

play02:51

inside their

play02:52

dependencies right so about before that

play02:54

yeah

play02:55

we right click our controller and then

play02:58

we add a new item

play03:04

so i'm just gonna select airspeed.net

play03:06

call so what we do is we're going to add

play03:09

an

play03:10

empty api controller

play03:14

so here as you can see we've got an

play03:16

empty api

play03:18

controller which is values controller

play03:20

right

play03:21

so once we've added the controller we've

play03:23

got values controller so we actually

play03:25

expand the properties then the launch

play03:28

settings

play03:30

so here as you can see we've got um the

play03:33

launch url

play03:34

to be weather forecast so we're going to

play03:36

change this bit and

play03:37

we've got the values controller so we're

play03:39

just going to put values here instead

play03:41

right so that's all we're going to do

play03:44

for this bit we're just going to save

play03:45

the file

play03:46

and we can actually close it right so

play03:49

the next bit obviously we're just going

play03:50

to add a new

play03:51

one more project and this project will

play03:54

be

play03:55

obviously a console app so it's a c

play03:58

sharp console app right

play04:01

and this project will be obviously

play04:05

let's say web api

play04:12

client

play04:15

right so we add this as well we

play04:19

click on the create and we should get

play04:22

a console app created for us yeah so

play04:25

as we can see here now the project is

play04:27

all set to run

play04:28

only the web api server so

play04:32

whilst we're here we're just going to

play04:33

right click on the solution

play04:35

and then we come to the set startup

play04:38

project

play04:39

so as you can see i will click on the

play04:41

multiple star project here

play04:43

and then we we actually put both of them

play04:46

on the start so that we can get both of

play04:48

them started at the same time

play04:50

so here we put start i saw we

play04:54

apply the changes so as you can see the

play04:57

changes

play04:57

has been actually reflected here so

play04:59

we've got multiple startup projects here

play05:02

yeah so now what we do is we come inside

play05:06

the values controller we're just going

play05:08

to put nothing fancy for here

play05:10

so we're just going to make a very

play05:11

simple get method

play05:13

so it's http get yeah

play05:18

so we're just going to call this um

play05:20

public

play05:23

um string we're just gonna make some

play05:25

string

play05:26

array and what we call it is gets

play05:32

so here what we do is we only are going

play05:35

to return

play05:37

a new instance of a string array

play05:42

so we're just going to put some date as

play05:44

in some day i

play05:46

string this inside here so the first day

play05:49

obviously i'm just going to put mango

play05:52

the nest

play05:55

i mean this is entirely up to you so you

play05:57

can put whatever you want

play05:59

but for this video i'm just gonna make

play06:01

it as simple as possible

play06:03

so we're just gonna put banana

play06:06

and we're gonna pop her

play06:11

and one more we're just gonna pour one

play06:13

more

play06:15

uh but um apple

play06:28

just a moment please okay so

play06:31

i mean i've actually got the comment

play06:33

inside so i'm just going to pull it out

play06:34

here

play06:35

then then one

play06:38

should go away so this is all we've

play06:40

actually got there the get method so

play06:42

what we do is we come inside

play06:46

the client to web api client we open the

play06:48

program

play06:49

so here so right after the console.

play06:54

right line here what we do is just gonna

play06:55

put another another console.rt line here

play07:02

so what we do is we're going to start by

play07:05

using

play07:08

so we're just going to put first

play07:14

console the right line so here

play07:19

we're just going to say press enter key

play07:22

to continue

play07:28

so what we do is we're just going to put

play07:30

another console.red line here

play07:40

and then what we do is we use the using

play07:42

keyword so we'll be using

play07:46

we're going to use http

play07:53

client which will be clark clients

play07:57

this will be according to the new

play08:01

instance of our http client right

play08:06

so here what we do is we actually

play08:08

reference it

play08:12

so we're going to use the the

play08:16

system.net.http.http client

play08:19

yeah so we're going to use the same for

play08:20

this as well

play08:24

system.html

play08:26

yeah so that's what we're going to be

play08:29

using now so what we do is we just put

play08:32

inside a bracket so what we do is we

play08:35

initialize a variable called

play08:39

response this will be equal to so this

play08:42

method what we do

play08:43

is we're going to make the the main

play08:45

method

play08:47

async task

play08:50

so here we're going to reference

play08:55

system.task so

play08:58

inside the response we're going to we

play09:01

can await

play09:02

yeah so we could say client

play09:06

dot

play09:09

get async

play09:14

so we can put our uri

play09:17

inside here so what we do is i'm just

play09:19

going to make

play09:20

the server to start up

play09:24

just to start up yeah i'm going to set

play09:27

it to start up

play09:29

so you'll be just a server that will run

play09:31

so we can get the uri

play09:38

so actually before you can run with the

play09:40

runner there's one more thing that

play09:42

i need to show you

play09:45

so we come inside the view values

play09:47

controller so

play09:49

basically here if you've got the slash

play09:52

api just remove

play09:53

that bit from the controller

play10:16

uri yes so we stop it from running

play10:21

then we come inside uh the programs are

play10:24

cs code

play10:25

so inside here we can paste the uri that

play10:28

we just copied

play10:30

inside here yeah

play10:33

so what we can do is we can say

play10:37

response dot ensure

play10:40

success code

play10:44

and then we can say if

play10:48

response does is

play10:52

success to success status code

play10:56

that means we know that there is

play10:59

successful the response is successful so

play11:01

this is what we would do

play11:03

we going to initialize a string variable

play11:06

we know

play11:07

it's a string array but we just we could

play11:10

convert it to a string gallery but we're

play11:11

just going to leave that for this video

play11:13

so we could say

play11:15

await response

play11:19

dot content dot

play11:22

reader string async

play11:26

yeah so the next bit is obviously we

play11:30

could say

play11:31

console or rail line then we can put a

play11:33

message inside the console

play11:35

yeah so after that what we could just

play11:39

do is else we put the else statement

play11:41

here

play11:44

so we could say console.rightline

play11:50

a response

play11:53

oh we could just leave it here

play11:57

something like this you can just write

play11:58

something like this so

play12:02

response error code

play12:06

will be so we can put the response code

play12:09

here so if we say

play12:11

response dot

play12:15

status code yeah so that's what we're

play12:19

going to leave for here for now

play12:21

so as you can see it's very simple stuff

play12:23

so what we do is we actually turn it

play12:25

straight back to running the

play12:26

the multiple projects so initialize

play12:29

startup projects

play12:30

so we've got them started already so

play12:33

apply changes

play12:34

and then we run it to see what actually

play12:36

happens

play12:39

so now we're just gonna wait for the so

play12:41

that one is actually loaded

play12:43

so we're just gonna put this bit here

play12:45

and

play12:46

actually get our console client

play12:50

so we could press enter key to enter so

play12:53

as you can see we've got the data now to

play12:55

be displayed

play12:57

here now so we could actually convert it

play12:59

to our rate

play13:00

it's just displaying a string so but you

play13:02

get you get

play13:03

what is going on here now so what i'm

play13:06

going to do is leave the video here for

play13:07

now

play13:09

so you can actually explore it further

play13:11

and build on

play13:12

what you've actually learned from here

play13:13

so once again

play13:15

if you haven't subscribed the channel

play13:16

please make sure you do because i've got

play13:18

lots of videos coming up

play13:24

stay blessed

Rate This
β˜…
β˜…
β˜…
β˜…
β˜…

5.0 / 5 (0 votes)

Related Tags
ASP.NETWeb APIVisual StudioTutorialsRESTful ServicesClient ServerHTTP ServicesCoding GuideAPI Development.NET FrameworkSoftware Development