1 00:00:00,280 --> 00:00:05,350 While the configuration of Web API might vary somewhat from project to project, 2 00:00:05,350 --> 00:00:09,150 every installation requires at least one route, 3 00:00:09,150 --> 00:00:12,920 often referred to as the default route, to be configured. 4 00:00:12,920 --> 00:00:15,070 If we don't configure at least one route, 5 00:00:15,070 --> 00:00:20,080 Web API won't know how to map incoming requests to controller action methods. 6 00:00:20,080 --> 00:00:22,744 In our Web API register static method, 7 00:00:22,744 --> 00:00:28,247 we can use the HTTP configuration objects routes property to configure a route. 8 00:00:31,236 --> 00:00:36,167 The routes property is of type HTTP route collection, 9 00:00:36,167 --> 00:00:43,578 which provides a method named maphttproute that can be used to map a route template 10 00:00:47,420 --> 00:00:51,993 When calling the map HTTP route method, you supply a name for the route template, 11 00:00:51,993 --> 00:00:56,237 the route template itself, and optionally any defaults or constraints for 12 00:00:56,237 --> 00:00:57,446 the route template. 13 00:00:59,780 --> 00:01:05,693 First, the name, name: "DefaultAPI", 14 00:01:05,693 --> 00:01:10,812 then routeTemplate For 15 00:01:10,812 --> 00:01:17,334 the route template, it's common to use for 16 00:01:17,334 --> 00:01:25,099 the default route api/{controller}/{id}. 17 00:01:25,099 --> 00:01:27,729 If you've configured routes for the MVC framework, 18 00:01:27,729 --> 00:01:30,160 this route template will look familiar. 19 00:01:30,160 --> 00:01:32,910 Let's break it down and review each of the route segments. 20 00:01:34,400 --> 00:01:37,320 The API literal path segment is to find 21 00:01:37,320 --> 00:01:42,300 in order to prevent collisions between web API and NVC routes. 22 00:01:42,300 --> 00:01:45,240 We currently aren't using NVC in our project, but 23 00:01:45,240 --> 00:01:49,760 I'm going to go ahead and include the API prefix on my Web API routes 24 00:01:49,760 --> 00:01:51,500 just in case that changes in the future. 25 00:01:54,230 --> 00:01:58,500 The controller route parameter statement is used to identify the controller to 26 00:01:58,500 --> 00:01:59,890 instantiate. 27 00:01:59,890 --> 00:02:04,460 The controller suffix is automatically added to the routing controller name. 28 00:02:04,460 --> 00:02:07,640 The controller name should represent the name of the resource 29 00:02:07,640 --> 00:02:10,380 that the controller's action methods will operate against. 30 00:02:11,860 --> 00:02:16,310 The id route parameter is used when retrieving a single resource. 31 00:02:16,310 --> 00:02:21,530 The route parameter value is mapped to an action method parameter of the same name. 32 00:02:21,530 --> 00:02:25,910 Since the id route parameter is only used when retrieving single resources, 33 00:02:25,910 --> 00:02:28,170 we need to indicate that it's optional. 34 00:02:28,170 --> 00:02:32,580 We can do that by supplying an anonymous object for the defaults parameter and 35 00:02:32,580 --> 00:02:36,852 setting an ID property on the anonymous object to the enumeration value 36 00:02:36,852 --> 00:02:45,685 RouteParameter.Optional. 37 00:02:53,486 --> 00:02:56,640 And that completes our default route. 38 00:02:56,640 --> 00:02:59,760 Notice that unlike the typical default MVC route, 39 00:02:59,760 --> 00:03:04,590 the route template doesn't include a route parameter segment for the action name. 40 00:03:04,590 --> 00:03:08,710 Next, we'll see how the request HTTP method is used 41 00:03:08,710 --> 00:03:11,120 to determine the controller action method to call.