Creating a Web API Project

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
this is part 2 of a speedor net web api tutorial in this video we'll discuss creating a new asp.net web api project explore and understand the web api code auto-generated by visual studio for this course we'll be using Visual Studio 2015 so let's flip to visual studio here I have Visual Studio 2015 Enterprise Edition running as an administrator to create a new asp.net Web API project click on file new project and within the new project dialog box let's select visual C shaft which is present under installed and under templates and then from the middle pane select asp.net web application we are going to create this within the C Drive and let's name this Web API demo let's click OK on the subsequent screen we have the option to select the type of web application that we want to create do you want to create an empty web application web forms M easy Web API etc we want to create an asp.net Web API project so I'm going to select Web API and we don't want to host this in the cloud so let's keep this checkbox unchecked and click OK so this is going to take a few seconds to create the asp.net Web API project for us I am able to create the web API project successfully but just in case if you run into these two errors when creating the web API project that is package installation error or fail to initialize the PowerShell host then here are the steps to resolve those errors and successfully create a Web API project the first step is to close all instances of Visual Studio that are running on your machine the second step is to launch Windows PowerShell so to launch windows powershell click on start and then select all apps and then scroll all the way down to W and here you should find windows powershell folder and within that we have windows powershell so right-click on that and select run as administrator from the context menu if you get a secure ready prompt click yes and they should launch Windows PowerShell here you may get another security prompt now one of the choices there is to plus R which stands for run ones if you do get that security prompt press R and hit enter I don't have that security prompt so I don't have to do that and once you have pressed R and hit enter so at this prompt right here we have to execute a command and that is set execution policy and we want to set this to all signed and hit enter when the command is successfully execute the third step is to relaunch Visual Studio as an administrator and then we want to launch the package manager console window to do that click on tools and then NuGet package manager and here one of the choices our package manager console select that and this should open up the package manager console window here you may get another security prompt and one of the choices is to plus R which stands for run once and if you do get that security prompt plus R and hit enter and then attempt to create the asp.net Web API project using the same steps that we have just discussed and you should be able to create the Web API project just in case if you get other errors and if they are preventing you from creating the web API project leave those errors as comments on this video if anyone has got similar errors and if they have resolved them they may reply to you so here we have the Web API project created successfully now let's explore and understand the Web API code that is auto-generated by visual studio if you have worked with asp.net MVC in the past then this folder structure should be very familiar to you notice we have got folders for models views and controllers within the controllers folder we've got both MVC controllers and Web API controllers Web API controllers are different from MVC controllers the values controller is an asp.net Web API controller notice the values control up inherits from API controller class which is present in system dot web dot h t DB namespace the home controller that is present right here this is an MVC controller and notice the home controller class inherits from a different page class which is controller class and this class is present in system dot web dot MVC namespace notice in the values controller class we have get post put and delete methods these methods respond to get put post and delete HTTP verbs notice we have got two overloaded versions of the get method one without any parameters and the other one which take the ID parameter both of these methods respond to the get HTTP verb depending on whether a value for the ID parameter is specified in the URI or not now let's look at the default route that's in place for a web api project within our global dot a si X file we have application start even handler method this event is raised when the application starts notice here we have lot of configuration with configuration for filters bundles etc the one that we are interested in is the configuration for our web api and the configuration for web api is present in register method of the web api convict class so i'm going to right click on this register method and then select go to definition from the context menu which is going to take us to the register method in the web api convict class this class is present in app underscore start folder so notice here we have the Web API config dot CS file which contains over Web API convict class and the register method and within this register method we have the default route configured for our web api web api routes are different from MVC routes m easy routes are present in this file route config dot CS now if you look at the default route that's in place for web api it starts with the word api and then we have got a forward slash and then the name of the controller and then another forward slash and the optional ID parameter at this point let's run our project by pressing ctrl f5 which is going to take us to the default Start page now look at the default web api out it starts with the word api so within our URL right here localhost colon a port number and then for slash and then this word api and then another forward slash and then the name of our controller what is the name of our controller it is values controller we don't have to specify the full name of the controller it's enough if we just specify values so when we do that web api is going to take this word values and then append to that this word controller and then web api is going to look for a class with name values controller the browser right here is issuing a get request and this get request gets mapped to the get method within the values controller now with no values controller we have two overloaded versions of get method so which get method is going to respond to this URI now notice within the URI we don't have a value for the ID parameter so naturally this overload version of the get method which doesn't have a parameter is going to respond to the request so let's hit enter and see what we get notice we get a security error authorization has been denied for this request this is because of this authorized attribute right here which is naturally related to security we'll discuss this in detail in a later video to keep things simple for now let's keep it commented build of a solution and once the build completes successfully let's reissue the request now at the moment you know look at the URI right here it's API four slash values so Web API is going to look for values controller if you specify products here then it's going to look for products controller and if you look at the complete URI right here it's localhost colum a port number in a real world application it might be your domain in for example it could be HTTP : four slash four slash presume tech comm four slash API four slash values so at this point our build should have been succeeded let's reissue this request and if you look at what is this get method doing here it is returning to strings value 1 and value 2 so when this request results successfully we should get those two values in the browser so it's taking a few seconds since we have rebuilt our solution so as soon as the request completes successfully notice we have got value 1 and value 2 as expected we have another overloaded version of the get method that takes ID parameter if you remember with the default web api route that is in place the ID parameter is optional notice the defaults here ID parameter is optional and that's the reason we are able to call the get method without a value for the ID parameter in the URI now if we specify a value for the ID parameter in the URI then this overloaded version of the get method that has the ID parameter will be called and if you look at what this method is doing its simply returning the string value no matter whether you specify the value for the ID parameter s1 or 1000 we are simply going to get the string value so let's issue this request and see what we get notice we get the string value as expected what do you think is going to happen if the controller that we have specified is not found the controller that we have specified here is values so Web API is going to look for values controller and then since we are issuing a get request here the get method is going to respond to this request now let's see what's going to happen if we comment that the values controller so I'm going to comment this class completely let's see what changes build our solution as you can see in the status bar here the build completed successfully so let's reissue the request and see what we get look at the error message that we have got it's very meaningful no HTTP resource was found that matches the request URI for slash API four slash values 4/1 no type was found that matches the controller named values so basically it's saying it can't find values controller that we have specified in the URI right here which is we're remaining four in this video we've just seen how to perform the get request in our next video we'll discuss how to perform the rest of the actions that is put post and delete thank you for listening and have a great day
Info
Channel: kudvenkat
Views: 797,037
Rating: undefined out of 5
Keywords: asp.net web api uri mapping, how to create asp.net web api project, visual studio web api project, web api values controller, failed to initialize powershell host vs 2015, visual studio package installation error
Id: 6qwuFQDB2jU
Channel Id: undefined
Length: 11min 37sec (697 seconds)
Published: Mon Aug 29 2016
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.