1 00:00:00,540 --> 00:00:06,325 Our EntriesController action methods are currently using IEnumerable of type Entry, 2 00:00:06,325 --> 00:00:08,913 entry are void for their return types. 3 00:00:11,591 --> 00:00:16,297 When returning a type like IEnumerable of type Entry, or entry, 4 00:00:16,297 --> 00:00:21,348 Web API will serialize the return value to the response message body, 5 00:00:21,348 --> 00:00:25,305 and set the response's HTTP StatusCode to 200 OK. 6 00:00:29,184 --> 00:00:33,914 When returning void, Web API will simply return a response with 7 00:00:33,914 --> 00:00:37,760 an HTTP StatusCode of 204 No Content. 8 00:00:37,760 --> 00:00:41,792 When we were designing our API in the first section of this course, 9 00:00:41,792 --> 00:00:46,761 we discussed that we wanted our API to return appropriate responses with commonly 10 00:00:46,761 --> 00:00:48,287 used HTTP status codes. 11 00:00:52,630 --> 00:00:57,733 For example, in our second get action method if a resource isn't found for 12 00:00:57,733 --> 00:01:03,408 the provided ID parameter value, we should we turn a 404 Not Found StatusCode. 13 00:01:05,273 --> 00:01:10,895 And in the Post action method instead of returning a 204 No Content StatusCode, 14 00:01:10,895 --> 00:01:13,224 we should return 201 Created, 15 00:01:13,224 --> 00:01:18,110 indicating that the request led to the creation of a resource. 16 00:01:18,110 --> 00:01:21,770 Web API provides a set of built-in HTTP 17 00:01:21,770 --> 00:01:25,930 action result types that we can use to refine our responses. 18 00:01:25,930 --> 00:01:30,300 To make these types even easier to use the API controller base class, 19 00:01:30,300 --> 00:01:34,050 provides a set of helper methods that we can call that'll return 20 00:01:34,050 --> 00:01:36,790 one one of the HTTP action result types. 21 00:01:36,790 --> 00:01:41,359 For example, to return an OK negotiated content result, 22 00:01:41,359 --> 00:01:45,476 HTTP action result type, we can call the OK method. 23 00:01:53,820 --> 00:01:57,108 Passing in the content to write to the message body. 24 00:02:02,222 --> 00:02:08,395 Calling the OK method will return a response with a 200 OK HTTP StatusCode. 25 00:02:10,060 --> 00:02:13,937 Now that we're returning an HTTP action result type, 26 00:02:13,937 --> 00:02:19,093 we need to change our return type to the IHhttpActionResult interface. 27 00:02:27,413 --> 00:02:32,097 Under the covers, HTTPActionResult types handle the creation and 28 00:02:32,097 --> 00:02:36,110 configuration of an HTTP response message. 29 00:02:36,110 --> 00:02:40,820 And if you encounter a scenario that one of the built in HTTPActionResult types 30 00:02:40,820 --> 00:02:46,670 doesn't cover, you can create your own custom HTTPActionResult type. 31 00:02:46,670 --> 00:02:51,473 For a complete list of the API controllers, HTTPActionResult helper 32 00:02:51,473 --> 00:02:56,670 methods, or for more information on the HTTP response message type. 33 00:02:56,670 --> 00:03:00,950 Or how to create your own custom HTTPActionResult types, 34 00:03:00,950 --> 00:03:02,700 see the teacher's notes. 35 00:03:02,700 --> 00:03:05,813 Now, let's update our remaining control or action methods. 36 00:03:08,622 --> 00:03:13,177 In the second GET action method, let's handle the case when a resource isn't 37 00:03:13,177 --> 00:03:15,745 found for the provided ID parameter value. 38 00:03:17,243 --> 00:03:22,780 Let's start by changing the methods return typed IHttpActionResult. 39 00:03:22,780 --> 00:03:27,672 Then, let's capture our call to the entriesRepository.Get method into 40 00:03:27,672 --> 00:03:29,310 a variable named entry. 41 00:03:32,270 --> 00:03:35,115 Then we can check if the entry variable is null. 42 00:03:37,822 --> 00:03:43,180 And if it is null, we can return a call to the NotFound method. 43 00:03:43,180 --> 00:03:47,979 That'll return a response with a 404 NotFound HTTP StatusCode, 44 00:03:47,979 --> 00:03:52,200 then at the bottom of the method return a call to the Ok method. 45 00:03:53,730 --> 00:03:55,528 Passing then our entry variable. 46 00:03:58,151 --> 00:04:02,852 Moving on to the POST method start by changing the return type from 47 00:04:02,852 --> 00:04:04,995 void to IHttpActionResult. 48 00:04:06,420 --> 00:04:09,296 Then, simply return a call the to the Created method. 49 00:04:11,536 --> 00:04:18,250 The Created method will return or response with the 201 Created HTTP StatusCode. 50 00:04:18,250 --> 00:04:23,350 The Created method accepts two parameter values, a URI representing the location 51 00:04:23,350 --> 00:04:27,880 for the newly created resource, and the object for the resource itself. 52 00:04:27,880 --> 00:04:32,120 We can generate the URI by making a call to the Url.Link method. 53 00:04:35,002 --> 00:04:39,613 The first parameter that we pass to the link method is the name of 54 00:04:39,613 --> 00:04:44,054 the route template to use, which in our case is DefaultAPI. 55 00:04:44,054 --> 00:04:47,213 That's the name for the route template for our default route. 56 00:04:49,672 --> 00:04:53,247 For the second parameter, we need to pass an anonymous object for 57 00:04:53,247 --> 00:04:54,874 the route parameter values. 58 00:04:58,535 --> 00:05:03,191 Controller = the name of our controller. 59 00:05:05,546 --> 00:05:08,644 And id = entry.Id. 60 00:05:12,525 --> 00:05:16,530 For the created method second parameter, we need to pass in our entry object. 61 00:05:21,840 --> 00:05:26,610 The Put and Delete action methods are okay as they are, at least for now. 62 00:05:26,610 --> 00:05:29,778 We'll update the Put method along with the POST method again, 63 00:05:29,778 --> 00:05:33,257 later in this section when we handle implementing our validations. 64 00:05:36,921 --> 00:05:41,994 Leave in the delete method with the void return type will result in a response 65 00:05:41,994 --> 00:05:47,110 with an empty message body and an HTTP StatusCode of 204 No Content. 66 00:05:47,110 --> 00:05:49,688 Which happens to be an appropriate response for 67 00:05:49,688 --> 00:05:51,690 successfully deleting a resource. 68 00:05:51,690 --> 00:05:54,085 Let's test the changes to our entries controller. 69 00:06:03,201 --> 00:06:08,092 In Postman let's request a single resource from the entries endpoint. 70 00:06:08,092 --> 00:06:15,437 The path will be API/entries/1, and click the Send button. 71 00:06:17,641 --> 00:06:18,801 Down here on the bottom, 72 00:06:18,801 --> 00:06:21,960 we can see the JSON formatted data that was sent in the message body. 73 00:06:23,360 --> 00:06:27,892 And we can see that the responses HTTP StatusCode was 200 OK. 74 00:06:27,892 --> 00:06:33,156 Now let's request a single resource that we know won't exist, 75 00:06:33,156 --> 00:06:38,137 by providing an ID wrap-router value of let's say 999. 76 00:06:40,487 --> 00:06:44,057 Now we can see that the message body was empty, and 77 00:06:44,057 --> 00:06:47,882 the responses HTTP status code was 400 Not Found. 78 00:06:47,882 --> 00:06:50,933 Now let's try creating a new resource. 79 00:06:50,933 --> 00:06:55,136 Select POST in the list of HTTP methods, and 80 00:06:55,136 --> 00:06:58,893 provide the URI path, API/entries. 81 00:07:00,757 --> 00:07:04,229 Click on the Body tab, select raw, and for 82 00:07:04,229 --> 00:07:09,454 the content media type, select JSON(application/json). 83 00:07:11,832 --> 00:07:17,121 To save time I have the JSON formatted data for our message body here in Notepad. 84 00:07:17,121 --> 00:07:20,400 If you're following along, see the teacher's notes for a copy of this data. 85 00:07:22,783 --> 00:07:26,244 Click in the message body field, and paste from the clip board, 86 00:07:26,244 --> 00:07:27,794 then click the Send button. 87 00:07:31,171 --> 00:07:35,656 Scroll down to the see the response, and here's the message body containing 88 00:07:35,656 --> 00:07:38,416 the data for the new resource that was created. 89 00:07:41,882 --> 00:07:49,252 And we can see here that the Status was 201 Created Clicking on the Headers tab, 90 00:07:49,252 --> 00:07:53,880 allows us to see that the Location Header is set to the URI for the new resource.