Create Your First Web API Using Visual Studio With C# Beginners Guide

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey what's up everybody in this video I'm going to show you how to create your first restful api using Microsoft Visual Studio first thing we got to do is download Visual Studio and you can do that by simply going to Visual Studio calm and we are going to be using this version of Visual Studio now Visual Studio code or the version for Mac if you click on download Visual Studio you'll get three versions of it the last two professional and enterprise you are able to get a trial but you have to pay for those the community version is free to use indefinitely so let's go ahead and select that one since I already have it installed on my computer let's pretend that you are gonna go through the full installation so you can pause the video here and you can continue once you installed Visual Studio alright so now you have Visual Studio on your computer so once you open it it's gonna look something like this you'll probably won't have any projects on the left-hand side if you haven't used a visual studio before so the next thing we're gonna do is we're gonna create a new project we're going to search for web and we're going to select asp.net core web application then we're gonna press next let's change the place where we're gonna put it so let's put it on the C Drive let's select this folder and let's rename the application to first web api let's click create and we are going to select API which is already selected by default we're going to use the latest version of.net core we're going to configure for HTTP and they said the templates that are provided by Visual Studio out-of-the-box you can create react applications using redox or angular or if you want to build something for like a website that uses MVC you could also use a web application using the MVC pattern so let's go ahead and leave it as is we're not going to select any type of authentication configure for HTTP let's go create okay so let's close this as you can see Visual Studio created some files to get a quick start on the project so without making any changes you can go heading and run this Web API and you will actually be getting some data back which is just sample data as you can see we're using this route or that endpoint and there you have it you have created your first web api or restful api well thing i'm just kidding let's add more functionality to this let's go ahead and add another controller here gonna create a controller it's gonna be a an API controller and we're gonna call it home actually let's just call a hello okay let's go ahead and add another endpoint so this will be a this action will respond to get HTTP GET requests and it's simply going to return a string saying hello world and let's specify that this endpoint will respond to HTTP GET requests alright let's go ahead and run this okay by default the application or that API goes to this endpoint so let's switch it up and go to the new hello controller and we get an error and this is there let me explain to you why we're getting this error if we look at the weather forecast controller we specify this route so the controller piece will be replaced by the name of the controller which is weather forecast in the case of the hello controller we have specified or by default Visual Studio has specified that it's going to be located under API so it's gonna be the base URL or domain in this case localhost and API slash hello so let's go ahead and run it again and let's try that so the only change we got to do here is change this to API slash hello and there you go so now we are successfully responding to an HTTP GET request so let's let's add a little bit more functionality to this so now let's say that you want to send something to your web api or to your api so for that you need to make an HTTP POST request and let's set up an endpoint for that so we can specify this will respond to post call this action post and for this I wanna add something to make it easier to receive any kind of data so let's go to tools and nougat package manager and manage packages for this solution we're gonna browse for oh here we go Newton soft I didn't even have to look for it okay so let's select Newton soft we're gonna use it in the first wave a pea I project which is the only project a new solution in this case okay it's giving me an error because I didn't finish defining that post endpoint so that's fine we should already have mutant soft installed so let's go ahead and use some of its functionality let's say that we are going to get a J object let's create or let's enter the using statement there we go so we're going to receive a JSON object orders in this case a J object is what they call it and we are simply going to return whatever we receive I just want to make a note this is not secure this is not recommendable on a production scenario because you could be exposed to you know some type of attacks because you're not validating the payload that you are getting in but for this tutorial this is gonna do well maybe in future videos I will get more into security and how you can prevent those attacks for testing this I use a tool called postman and you can get that tool by going to you guessed a postman com download the app and then select whether you want a 32-bit version of the 64-bit version so again you can pass a video here go ahead download it install it and you can continue once you have installed postman okay so now that you have postman installed you can open it up and it's gonna look something like this let's go ahead and create a new request and this is going to be a post request and here we're gonna enter the URL so let's run the project so I can get that usually every time you start a new project rate a new project in Visual Studio you'll get a random port number where that application will be running so that's why I need to copy and paste this because I didn't set that up you can manually go in and set it up too for it to run on any port that you want but in this case it's running in this on this port so let's get that URL let's paste it here and we're gonna be sending it to API hello okay let's test it okay so it seems like we're getting an issue and the reason why we're getting this issue is because when we created the project we selected to use HTTP what's happening is that your computer is self signing a certificate which is not valid to be used by a browser so we have to actually it works fine on the browser since your computer already signed it but postman it's a little bit more secured and you have to have a valid certificate to be able to make request to to an API but we can disable that we can go to like it says here it's telling us to ignore or to not verify SSL certificate so let's turn that off Settings General and do not verify SSL Certificates you might wanna turn this back on after this demo I wouldn't recommend that you leave it but just to be able to test for this video let's turn that off okay so now let's make a request okay so now it's telling us that the media type that we're sending is unsupported we have to specify what kind of data we are sending into this API so let's go ahead and define that content type is gonna be application JSON and while we're here let's send some kind of payload and we're just gonna send a property called value okay let's send that okay so it looks like we have an issue here and I think I might have an idea why we're getting this issue so let's see [Music] okay I think we're missing this package here all right let's say let's tell the application that we are going to be using this okay let's go back to our controller and let's test it okay so now it's reaching this point and you can see that now we have a JSON object there you have it this is your first restful api with the get impulse call in future videos I might add the functionality to connect to a database and save the changes or maybe authorizing another authenticating out the ricing requests or maybe creating custom names or routes please leave comments with suggestions of what the next video should cover and we'll see you on the next one
Info
Channel: thedevlife
Views: 154,229
Rating: undefined out of 5
Keywords: web api, restful api, rest api, api, asp.net, .net core, .net core api, .net core web api, c# api, visual studio, asp.net mvc, c# training, c# tutorial for beginners, asp.net mvc project, web api c#, web api asp.net core, restful api c#, restful api design, rest api explained, asp.net web api, rest api c#, c# api development, c# api tutorial for beginners, web api tutorial, rest api tutorial, thedevlife, .net, c#, asp.net core web api, api development in c#
Id: JpZIzdzvemU
Channel Id: undefined
Length: 14min 19sec (859 seconds)
Published: Thu Apr 09 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.