Adding Web API to Our Project6:12 with James Churchill
Now that we've been introduced to REST APIs and the Web API framework, we're ready to work on our API.
To follow along committing your changes to this course, you'll need to fork the aspnet-fitness-frog-spa repo. Then you can clone, commit, and push your changes to your fork like this:
git clone <your-fork> cd aspnet-fitness-frog-spa git checkout tags/v2.1 -b adding-web-api
[MUSIC] 0:00 Now that we've been introduced to REST APIs and the WEB API framework, 0:04 we're ready to work on our API. 0:09 Let's get started. 0:11 There are two options for getting started with the WEB API framework. 0:13 Creating a new project with the necessary dependencies and configuration. 0:17 We're adding those things to an existing project. 0:21 When creating a new ASP.NET project, you actually have two options. 0:28 The first option, is to use the empty project template and 0:33 select to add the Web API folders and core references to your project. 0:37 Your project won't contain any API controllers, but 0:44 it'll have everything that you need to get started with adding your first controller. 0:47 The second option, is to select the Web API project template. 0:51 This will give you a project that not only contains the Web API folders and 0:56 core references, but also a single example controller. 0:59 The project will also contain an example of how to combine Web API and 1:03 MVC, to provide help pages for your API. 1:08 For more information on how to provide help pages for 1:11 your API, see the teacher's notes. 1:14 Since we're starting with an existing project, 1:17 we need to add the necessary dependencies and configuration to our project. 1:19 To do that, we can use the NuGet package manager. 1:23 If we browse for packages and scroll down in the list of it, 1:29 We should see the Microsoft.AspNet.WebApi package. 1:38 If you don't see it in this list, try typing WebApi in the search box. 1:43 At the time of this recording, the latest stable version is 5.2.3, 1:48 which is actually WebApi 2.2. 1:53 Once you've found the correct package, go ahead and install it. 1:56 Web API itself is dependent on a number of other packages. 2:02 Newtonsoft.Json, AspNet.WebAPI.Client, 2:07 AspNet.WebAPI.Core, and AspNet.WebAPI.WebHost. 2:12 Go ahead and click OK, and accept the end user agreement. 2:21 Now that we've installed Web API, 2:34 we need to define a static method to configure Web API. 2:36 First, add a folder named App_Start, Add, New Folder. 2:40 Then add a new C# class named Web API Config to the App_Start folder. 2:51 Go ahead and remove App_Start from the end of the namespace. 3:04 Typically, the convention is for 3:10 the last segment of the namespace to match the folder name. 3:12 But for classes located in the ASP.NET App_Start folder, 3:15 the convention is to use the project's route namespace. 3:18 Doing this will make it slightly easier to reference our static method 3:22 from the Global.asax CS file. 3:26 The WebApiConfig class will only contain a single static method. 3:30 So let's make the class itself static 3:34 by adding the static keyword right before the class keyword. 3:36 Doing this will prevent the class from ever been instantiated. 3:41 Now, add the static method named registered, 3:45 public static void Register, 3:51 And a parameter of type HttpConfiguration. 3:57 Add a using statement for the System Web.Http namespace. 4:06 And name the parameter, config. 4:12 In later videos, we'll add code to this method to configure our API. 4:15 Now that we have our API configuration method, 4:20 we need to ensure that it'll get called when an application is starting up. 4:22 We can do that by updating the Global.asax CS file located in the root 4:27 of our project. 4:31 The Global.asax CS file defines a class containing a method named, 4:36 Application_Start, which is called every time that the web app is started. 4:41 To ensure that our configuration method will get called at the right time 4:47 when the Web API framework is being initialized, we call the global 4:51 configuration, Configure method, and pass a reference to our configuration method. 4:55 Remember, WebApiConfig is the name of the static class. 5:23 And Register is the name of our static configuration method. 5:28 By not including a set of parentheses here, 5:32 we're passing a reference to the method instead of calling it. 5:34 Our project isn't using the MVC web framework. 5:38 But if we were adding WebApi to an existing MVC project, 5:41 we'd need to make sure to call the global configuration, Configure method, 5:44 before calling the method that configures the routes for MVC. 5:49 The easiest way to ensure that happens is to always place this method call at 5:53 the top of the Application_Start method. 5:57 Okay, we've got our Configuration method stubbed out, but 6:04 it's not doing anything yet. 6:07 Let's fix that next. 6:09
You need to sign up for Treehouse in order to download course files.Sign up