1 00:00:00,540 --> 00:00:05,640 After configuring our default route, we're ready to at our fist API controller. 2 00:00:05,640 --> 00:00:10,350 But before we do that, let's do a quick review of REST API endpoints, and 3 00:00:10,350 --> 00:00:17,150 how HTTP methods are use to specify the action to be taken against a resource. 4 00:00:17,150 --> 00:00:22,420 When working with REST API, HTTP requests are make against endpoints. 5 00:00:22,420 --> 00:00:28,390 When using the Web API framework, API controllers represent our endpoints. 6 00:00:28,390 --> 00:00:33,830 Each request is also associated with an HTTP verb or method. 7 00:00:33,830 --> 00:00:36,461 These are the actions that can be taken against resources. 8 00:00:37,786 --> 00:00:43,040 There are four main HTTP methods that are used with REST APIs. 9 00:00:43,040 --> 00:00:49,080 The GET HTTP method fetches a collection of resources, or a single resource. 10 00:00:49,080 --> 00:00:52,280 POST adds a new resource to a collection. 11 00:00:52,280 --> 00:00:56,470 PUT updates a resource, and DELETE deletes a resource. 12 00:00:57,580 --> 00:01:01,750 When mapping a request to a controller action method, Web API, 13 00:01:01,750 --> 00:01:06,520 by default, will look for a controller action method, who's name matches or 14 00:01:06,520 --> 00:01:08,870 starts with the HTTP method name. 15 00:01:10,160 --> 00:01:14,870 Let's add our first API controller, and see all of this in action. 16 00:01:14,870 --> 00:01:19,190 Just as with MVC projects, it's a convention though not an absolute 17 00:01:19,190 --> 00:01:23,630 requirement to put all of your API controllers in a folder named controllers. 18 00:01:30,507 --> 00:01:34,622 When working in a project that's using both MVC and Web API, 19 00:01:34,622 --> 00:01:39,221 I typically put MVC controllers in a folder named Controllers, and 20 00:01:39,221 --> 00:01:43,240 API controllers in a folder named API Controllers. 21 00:01:43,240 --> 00:01:45,213 But again, that's not a requirement. 22 00:01:45,213 --> 00:01:49,360 It's just a convention that I find helps me to quickly find the controller that I'm 23 00:01:49,360 --> 00:01:50,700 looking for. 24 00:01:50,700 --> 00:01:54,425 Let's add a class to the Controllers folder named EntriesController. 25 00:02:01,441 --> 00:02:06,653 When web API is attempting to resolve or route to a controller action method, 26 00:02:06,653 --> 00:02:10,901 it looks for a class that has a suffix of controller is public and 27 00:02:10,901 --> 00:02:15,170 non-abstract and implements IHttpController. 28 00:02:15,170 --> 00:02:17,740 Our class name contains the controller suffix. 29 00:02:19,200 --> 00:02:23,972 And it's public and non-abstract, so we're good on those counts. 30 00:02:23,972 --> 00:02:28,710 But it currently doesn't implement the IHttpController interface. 31 00:02:28,710 --> 00:02:30,750 To satisfy that requirement, 32 00:02:30,750 --> 00:02:34,600 we can inherit from Web APIs ApiController base class. 33 00:02:39,534 --> 00:02:44,570 Be sure to add add a using directive for the System.Web.Http namespace. 34 00:02:46,728 --> 00:02:50,850 If we navigate to the definition for the ApiController base class, 35 00:02:50,850 --> 00:02:54,697 we can see that it implements the IHttpController interface. 36 00:02:58,178 --> 00:03:02,771 The ApiController base class also defines a number of helper methods that we'll make 37 00:03:02,771 --> 00:03:04,310 use of later in the section. 38 00:03:09,827 --> 00:03:12,770 Now, let's step out our controller's action methods. 39 00:03:15,898 --> 00:03:18,838 To review, our API design calls for 40 00:03:18,838 --> 00:03:23,640 us to support the GET, POST, PUT, DELETE HTTP methods. 41 00:03:26,463 --> 00:03:32,150 So to start, I'll add four methods with names to match those HTTP methods. 42 00:03:35,694 --> 00:03:38,244 Public void 43 00:03:38,244 --> 00:03:47,439 Get Public void Post. 44 00:03:50,976 --> 00:03:51,917 Public void Put. 45 00:03:55,777 --> 00:03:57,751 And public void Delete. 46 00:04:00,599 --> 00:04:03,380 We actually need two Get methods. 47 00:04:03,380 --> 00:04:05,860 One that will return a collection of resources and 48 00:04:05,860 --> 00:04:08,040 one that will return a single resource. 49 00:04:13,435 --> 00:04:18,674 For the Get method, that will return a single resource, allot a parameter 50 00:04:18,674 --> 00:04:24,160 named id of type int, to represent the id of the resource to return. 51 00:04:24,160 --> 00:04:27,494 Get requests are intended to retrieve resources, so 52 00:04:27,494 --> 00:04:30,170 these methods should have return values. 53 00:04:36,600 --> 00:04:41,234 For now, let's use IEnumerable of type Entry, 54 00:04:45,189 --> 00:04:47,141 And Entry. 55 00:04:50,783 --> 00:04:54,460 For the return types and null for the return values. 56 00:05:02,118 --> 00:05:08,108 Entry is a model class that represents an entry's resource in our application. 57 00:05:08,108 --> 00:05:12,270 Later in the section, we'll see how it can use Web API response types and 58 00:05:12,270 --> 00:05:17,010 helper methods in the ApicController base class, til we find our responses. 59 00:05:17,010 --> 00:05:22,340 We've named our action methods so that the match the HTTP method names exactly. 60 00:05:22,340 --> 00:05:28,210 But in reality, the method names just need to start with the HTTP method name. 61 00:05:28,210 --> 00:05:32,760 For example, we could rename our first Get method to GetEntries. 62 00:05:38,267 --> 00:05:41,020 And the second Get method to GetEntry. 63 00:05:44,454 --> 00:05:48,690 I prefer to use the exact names, it's more concise. 64 00:05:48,690 --> 00:05:51,210 Which approach you use is something that you and 65 00:05:51,210 --> 00:05:55,360 your team should discuss, decide, and follow consistently. 66 00:05:55,360 --> 00:05:59,060 If you prefer, you can also ignore the naming convention completely. 67 00:05:59,060 --> 00:06:02,899 And use a configuration based approach by decorating your 68 00:06:02,899 --> 00:06:05,960 action methods with HTTP method attributes. 69 00:06:09,331 --> 00:06:11,990 HttpGet for Get request. 70 00:06:18,413 --> 00:06:21,020 HttpPost for Post request. 71 00:06:23,630 --> 00:06:26,160 HttpPut for Put requests. 72 00:06:28,003 --> 00:06:32,380 And, HttpDelete for Delete requests. 73 00:06:37,861 --> 00:06:40,140 When using the HTTP method attributes, 74 00:06:40,140 --> 00:06:43,300 you can name your action methods whatever you'd like. 75 00:06:47,964 --> 00:06:51,177 But, unless there's a compelling reason to do so, 76 00:06:51,177 --> 00:06:54,470 I'd still use the corresponding HTTP method names. 77 00:07:03,415 --> 00:07:06,583 Let's update the Get action method to return some data so 78 00:07:06,583 --> 00:07:08,895 that we can test our entries controller. 79 00:07:11,019 --> 00:07:13,855 First, let's define a biking activity. 80 00:07:30,356 --> 00:07:33,350 And then, return a list of type entry. 81 00:07:40,993 --> 00:07:43,491 Now, let's add some entry objects to our list collection. 82 00:07:43,491 --> 00:07:48,977 New Entry, the year, the month, the day, 83 00:07:48,977 --> 00:07:54,318 and our activityBiking and the duration, 84 00:07:54,318 --> 00:08:01,010 10.0m, which indicates that 10 is a decimal. 85 00:08:04,357 --> 00:08:08,197 Now copy and paste that entry. 86 00:08:08,197 --> 00:08:14,222 Change the date to 2017, 1, 3. 87 00:08:14,222 --> 00:08:19,179 Same activity and change the duration to 12, 12.2. 88 00:08:24,058 --> 00:08:27,834 Now that our Get method is returning some data, we're ready to run and 89 00:08:27,834 --> 00:08:29,120 test our application.