Adding an API Controller8:29 with James Churchill
Now we're ready to add our first API controller!
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.3 -b adding-an-api-controller
After configuring our default route, we're ready to at our fist API controller. 0:00 But before we do that, let's do a quick review of REST API endpoints, and 0:05 how HTTP methods are use to specify the action to be taken against a resource. 0:10 When working with REST API, HTTP requests are make against endpoints. 0:17 When using the Web API framework, API controllers represent our endpoints. 0:22 Each request is also associated with an HTTP verb or method. 0:28 These are the actions that can be taken against resources. 0:33 There are four main HTTP methods that are used with REST APIs. 0:37 The GET HTTP method fetches a collection of resources, or a single resource. 0:43 POST adds a new resource to a collection. 0:49 PUT updates a resource, and DELETE deletes a resource. 0:52 When mapping a request to a controller action method, Web API, 0:57 by default, will look for a controller action method, who's name matches or 1:01 starts with the HTTP method name. 1:06 Let's add our first API controller, and see all of this in action. 1:10 Just as with MVC projects, it's a convention though not an absolute 1:14 requirement to put all of your API controllers in a folder named controllers. 1:19 When working in a project that's using both MVC and Web API, 1:30 I typically put MVC controllers in a folder named Controllers, and 1:34 API controllers in a folder named API Controllers. 1:39 But again, that's not a requirement. 1:43 It's just a convention that I find helps me to quickly find the controller that I'm 1:45 looking for. 1:49 Let's add a class to the Controllers folder named EntriesController. 1:50 When web API is attempting to resolve or route to a controller action method, 2:01 it looks for a class that has a suffix of controller is public and 2:06 non-abstract and implements IHttpController. 2:10 Our class name contains the controller suffix. 2:15 And it's public and non-abstract, so we're good on those counts. 2:19 But it currently doesn't implement the IHttpController interface. 2:23 To satisfy that requirement, 2:28 we can inherit from Web APIs ApiController base class. 2:30 Be sure to add add a using directive for the System.Web.Http namespace. 2:39 If we navigate to the definition for the ApiController base class, 2:46 we can see that it implements the IHttpController interface. 2:50 The ApiController base class also defines a number of helper methods that we'll make 2:58 use of later in the section. 3:02 Now, let's step out our controller's action methods. 3:09 To review, our API design calls for 3:15 us to support the GET, POST, PUT, DELETE HTTP methods. 3:18 So to start, I'll add four methods with names to match those HTTP methods. 3:26 Public void 3:35 Get Public void Post. 3:38 Public void Put. 3:50 And public void Delete. 3:55 We actually need two Get methods. 4:00 One that will return a collection of resources and 4:03 one that will return a single resource. 4:05 For the Get method, that will return a single resource, allot a parameter 4:13 named id of type int, to represent the id of the resource to return. 4:18 Get requests are intended to retrieve resources, so 4:24 these methods should have return values. 4:27 For now, let's use IEnumerable of type Entry, 4:36 And Entry. 4:45 For the return types and null for the return values. 4:50 Entry is a model class that represents an entry's resource in our application. 5:02 Later in the section, we'll see how it can use Web API response types and 5:08 helper methods in the ApicController base class, til we find our responses. 5:12 We've named our action methods so that the match the HTTP method names exactly. 5:17 But in reality, the method names just need to start with the HTTP method name. 5:22 For example, we could rename our first Get method to GetEntries. 5:28 And the second Get method to GetEntry. 5:38 I prefer to use the exact names, it's more concise. 5:44 Which approach you use is something that you and 5:48 your team should discuss, decide, and follow consistently. 5:51 If you prefer, you can also ignore the naming convention completely. 5:55 And use a configuration based approach by decorating your 5:59 action methods with HTTP method attributes. 6:02 HttpGet for Get request. 6:09 HttpPost for Post request. 6:18 HttpPut for Put requests. 6:23 And, HttpDelete for Delete requests. 6:28 When using the HTTP method attributes, 6:37 you can name your action methods whatever you'd like. 6:40 But, unless there's a compelling reason to do so, 6:47 I'd still use the corresponding HTTP method names. 6:51 Let's update the Get action method to return some data so 7:03 that we can test our entries controller. 7:06 First, let's define a biking activity. 7:11 And then, return a list of type entry. 7:30 Now, let's add some entry objects to our list collection. 7:40 New Entry, the year, the month, the day, 7:43 and our activityBiking and the duration, 7:48 10.0m, which indicates that 10 is a decimal. 7:54 Now copy and paste that entry. 8:04 Change the date to 2017, 1, 3. 8:08 Same activity and change the duration to 12, 12.2. 8:14 Now that our Get method is returning some data, we're ready to run and 8:24 test our application. 8:27
You need to sign up for Treehouse in order to download course files.Sign up