1 00:00:00,580 --> 00:00:05,160 Browsing to the root of our website still returning a 404 not found there. 2 00:00:05,160 --> 00:00:10,270 We have our comic books list page now but NVC is still looking for 3 00:00:10,270 --> 00:00:14,850 an action method on a controller that doesn't exist in our project. 4 00:00:14,850 --> 00:00:19,320 So how do we make our comic books list you our home page? 5 00:00:19,320 --> 00:00:25,140 Remember, when we don't provide a URL path, NVC is defaulting the controller and 6 00:00:25,140 --> 00:00:29,640 action route values to home and index respectively. 7 00:00:29,640 --> 00:00:33,230 These defaults are defined in our default route. 8 00:00:33,230 --> 00:00:36,940 Every NVC website has a default route. 9 00:00:36,940 --> 00:00:41,640 When we created our NVC project, the Visual Studio tip that we used 10 00:00:41,640 --> 00:00:45,010 generated code to define our default route. 11 00:00:45,010 --> 00:00:46,920 So where can we find this code? 12 00:00:48,020 --> 00:00:51,120 To answer this question let's start by taking a look 13 00:00:51,120 --> 00:00:55,620 at the ASP.net global.asax file. 14 00:00:55,620 --> 00:01:00,390 The global.ASCX file, which is sometimes called the application file, 15 00:01:00,390 --> 00:01:02,950 is located in the root of our project. 16 00:01:02,950 --> 00:01:05,670 It contains global or application level code. 17 00:01:06,670 --> 00:01:10,530 Double click the file in the solution explorer to open it. 18 00:01:10,530 --> 00:01:15,570 Our global.ASCX file has just one method, Application_Start. 19 00:01:15,570 --> 00:01:18,800 The application start method is called once, and 20 00:01:18,800 --> 00:01:22,550 only once when our MVC website is being started. 21 00:01:22,550 --> 00:01:27,610 We can use this method to run any code that's needed to initialize our website. 22 00:01:27,610 --> 00:01:32,950 The first line of code is used to register all the MVC areas in our website 23 00:01:32,950 --> 00:01:37,320 MVC areas can be used to partition large MVC websites 24 00:01:37,320 --> 00:01:39,720 into smaller functional groupings. 25 00:01:39,720 --> 00:01:44,030 We aren't going to cover how to create or to use areas in this course. 26 00:01:44,030 --> 00:01:47,560 For more information on MVC areas, see the teacher's notes for 27 00:01:47,560 --> 00:01:49,980 links to additional resources. 28 00:01:49,980 --> 00:01:54,400 The second line of code is used to register the routes for our website. 29 00:01:54,400 --> 00:01:56,010 Right click on route config and 30 00:01:56,010 --> 00:02:00,600 select the go to definition menu item to navigate to that class. 31 00:02:00,600 --> 00:02:04,860 Let's collapse the solution explorer panel to give us more room in the editor. 32 00:02:04,860 --> 00:02:09,860 The route config class contains just a single register routes static method. 33 00:02:09,860 --> 00:02:13,990 Remember, by declaring a method to be static it can be called directly on 34 00:02:13,990 --> 00:02:18,440 the class itself without having to create an object instance first 35 00:02:18,440 --> 00:02:22,270 The RegisterRoutes method accepts a RouteCollection object 36 00:02:22,270 --> 00:02:25,420 which we can use to configure the routes for our website. 37 00:02:25,420 --> 00:02:29,280 Routes are defined by creating a series of rules. 38 00:02:29,280 --> 00:02:31,630 Our first rule specifies that requests for 39 00:02:31,630 --> 00:02:37,388 files that have an extension of .axd should be ignored by the routing engine. 40 00:02:37,388 --> 00:02:41,710 Requests for AXD files need to be handled by asp.net and 41 00:02:41,710 --> 00:02:45,010 should never be routed to one of our website's controllers. 42 00:02:45,010 --> 00:02:47,390 Including this routing rule accomplishes that. 43 00:02:47,390 --> 00:02:51,870 The second rule, is the definition for our default route. 44 00:02:51,870 --> 00:02:56,480 The order of the rules is important as MVC will apply the routing rules to 45 00:02:56,480 --> 00:03:01,270 incoming requests in the order that they appear in the route collection object. 46 00:03:01,270 --> 00:03:04,920 When we define the route we supply three things. 47 00:03:04,920 --> 00:03:09,840 The name of the route, the URL pattern that the route should match, and 48 00:03:09,840 --> 00:03:13,430 the default values for the routes UrlParameters. 49 00:03:13,430 --> 00:03:16,750 UrlParameters are defined in the URL pattern 50 00:03:16,750 --> 00:03:20,100 by surrounding the parameter names of curly braces. 51 00:03:20,100 --> 00:03:25,634 So, our default route has three parameters, controller, action, and ID. 52 00:03:25,634 --> 00:03:29,930 For each URL parameter in our route 53 00:03:29,930 --> 00:03:33,850 we can supply a default value or declare that it's optional. 54 00:03:33,850 --> 00:03:38,380 We can see here that our controller parameter has a default of home and 55 00:03:38,380 --> 00:03:42,010 the action parameter has a default of index. 56 00:03:42,010 --> 00:03:44,880 The ID parameter is declared as optional. 57 00:03:44,880 --> 00:03:48,080 Meaning that our route will match a request URL, 58 00:03:48,080 --> 00:03:51,080 even if it doesn't have a segment for the ID parameter. 59 00:03:51,080 --> 00:03:54,510 We've seen that behavior and action throughout this course, 60 00:03:54,510 --> 00:03:58,305 our website currently just has this one route definition. 61 00:03:58,305 --> 00:04:01,840 MVC allows us to define as many routes as we need. 62 00:04:01,840 --> 00:04:06,650 Though the majority of MVC websites never need anything other than the default route 63 00:04:06,650 --> 00:04:07,565 that you see here. 64 00:04:07,565 --> 00:04:12,310 Non-default routes are often referred to as custom routes. 65 00:04:12,310 --> 00:04:17,070 See the teacher's notes for links to additional resources about custom routes. 66 00:04:17,070 --> 00:04:19,500 Let's bring it back to our original question. 67 00:04:19,500 --> 00:04:23,470 How do we make our comic books list view our home page? 68 00:04:23,470 --> 00:04:26,140 It turns out to be relatively easy. 69 00:04:26,140 --> 00:04:30,550 In our default route definition, we just need to change the controller 70 00:04:30,550 --> 00:04:35,080 URL parameters to fault value from home to comic books. 71 00:04:35,080 --> 00:04:38,920 We can leave the action parameters to fault as it is. 72 00:04:38,920 --> 00:04:43,960 With that one change, NVC will now default the controller and action route values 73 00:04:43,960 --> 00:04:48,370 to comic books and index respectively, when we don't provide a URL path. 74 00:04:49,420 --> 00:04:54,760 Let's test our new default route by starting our website, and 75 00:04:54,760 --> 00:04:56,460 now our home page works. 76 00:04:57,510 --> 00:05:00,580 That's so much better than getting a 404 error. 77 00:05:00,580 --> 00:05:05,507 If you're using GitHub, let's close Chrome, stop the website, and 78 00:05:05,507 --> 00:05:06,992 commit our changes. 79 00:05:10,504 --> 00:05:15,364 Enter a commit message of updated the default route, 80 00:05:15,364 --> 00:05:18,394 and click the Commit All button. 81 00:05:20,559 --> 00:05:25,260 I realize that it might feel like we took the long way around to fix our home page. 82 00:05:26,360 --> 00:05:31,540 When doing software development, it's often helpful, not to mention important, 83 00:05:31,540 --> 00:05:34,900 to understand the underlying mechanics first, 84 00:05:34,900 --> 00:05:37,510 before making a change to your website. 85 00:05:37,510 --> 00:05:38,890 Without a proper foundation, 86 00:05:38,890 --> 00:05:44,480 it could be very easy to overlook or misunderstand the effects of a change, 87 00:05:44,480 --> 00:05:47,900 even a simple change like the one that we did to fix our home page. 88 00:05:49,230 --> 00:05:53,790 We have another change to make before our comic books list view is complete. 89 00:05:53,790 --> 00:05:59,200 Allow the user to easily navigate from the list view to the detail view. 90 00:05:59,200 --> 00:06:03,040 But before we do that let's do a short quiz to help solidify, 91 00:06:03,040 --> 00:06:06,060 what you just learned about URL, routing and default routes.