Updating the Default Route6:07 with James Churchill
In this video, we’ll update our default route so that our “Home” page doesn’t return a 404 “not found” error.
To follow along commiting your changes to this course, you'll need to fork the aspnet-comic-book-gallery repo. Then you can clone, commit, and push your changes to your fork like this:
git clone <your-fork> cd aspnet-comic-book-gallery git checkout tags/v5.3 -b updating-the-default-route
For more information about the ASP.NET
Global.asax file, see this page on MSDN.
If you’re interested in learning more about ASP.NET MVC Areas, see this MSDN page.
For more information about ASP.NET routing or creating custom routes, see the following MSDN pages.
Browsing to the root of our website still returning a 404 not found there. 0:00 We have our comic books list page now but NVC is still looking for 0:05 an action method on a controller that doesn't exist in our project. 0:10 So how do we make our comic books list you our home page? 0:14 Remember, when we don't provide a URL path, NVC is defaulting the controller and 0:19 action route values to home and index respectively. 0:25 These defaults are defined in our default route. 0:29 Every NVC website has a default route. 0:33 When we created our NVC project, the Visual Studio tip that we used 0:36 generated code to define our default route. 0:41 So where can we find this code? 0:45 To answer this question let's start by taking a look 0:48 at the ASP.net global.asax file. 0:51 The global.ASCX file, which is sometimes called the application file, 0:55 is located in the root of our project. 1:00 It contains global or application level code. 1:02 Double click the file in the solution explorer to open it. 1:06 Our global.ASCX file has just one method, Application_Start. 1:10 The application start method is called once, and 1:15 only once when our MVC website is being started. 1:18 We can use this method to run any code that's needed to initialize our website. 1:22 The first line of code is used to register all the MVC areas in our website 1:27 MVC areas can be used to partition large MVC websites 1:32 into smaller functional groupings. 1:37 We aren't going to cover how to create or to use areas in this course. 1:39 For more information on MVC areas, see the teacher's notes for 1:44 links to additional resources. 1:47 The second line of code is used to register the routes for our website. 1:49 Right click on route config and 1:54 select the go to definition menu item to navigate to that class. 1:56 Let's collapse the solution explorer panel to give us more room in the editor. 2:00 The route config class contains just a single register routes static method. 2:04 Remember, by declaring a method to be static it can be called directly on 2:09 the class itself without having to create an object instance first 2:13 The RegisterRoutes method accepts a RouteCollection object 2:18 which we can use to configure the routes for our website. 2:22 Routes are defined by creating a series of rules. 2:25 Our first rule specifies that requests for 2:29 files that have an extension of .axd should be ignored by the routing engine. 2:31 Requests for AXD files need to be handled by asp.net and 2:37 should never be routed to one of our website's controllers. 2:41 Including this routing rule accomplishes that. 2:45 The second rule, is the definition for our default route. 2:47 The order of the rules is important as MVC will apply the routing rules to 2:51 incoming requests in the order that they appear in the route collection object. 2:56 When we define the route we supply three things. 3:01 The name of the route, the URL pattern that the route should match, and 3:04 the default values for the routes UrlParameters. 3:09 UrlParameters are defined in the URL pattern 3:13 by surrounding the parameter names of curly braces. 3:16 So, our default route has three parameters, controller, action, and ID. 3:20 For each URL parameter in our route 3:25 we can supply a default value or declare that it's optional. 3:29 We can see here that our controller parameter has a default of home and 3:33 the action parameter has a default of index. 3:38 The ID parameter is declared as optional. 3:42 Meaning that our route will match a request URL, 3:44 even if it doesn't have a segment for the ID parameter. 3:48 We've seen that behavior and action throughout this course, 3:51 our website currently just has this one route definition. 3:54 MVC allows us to define as many routes as we need. 3:58 Though the majority of MVC websites never need anything other than the default route 4:01 that you see here. 4:06 Non-default routes are often referred to as custom routes. 4:07 See the teacher's notes for links to additional resources about custom routes. 4:12 Let's bring it back to our original question. 4:17 How do we make our comic books list view our home page? 4:19 It turns out to be relatively easy. 4:23 In our default route definition, we just need to change the controller 4:26 URL parameters to fault value from home to comic books. 4:30 We can leave the action parameters to fault as it is. 4:35 With that one change, NVC will now default the controller and action route values 4:38 to comic books and index respectively, when we don't provide a URL path. 4:43 Let's test our new default route by starting our website, and 4:49 now our home page works. 4:54 That's so much better than getting a 404 error. 4:57 If you're using GitHub, let's close Chrome, stop the website, and 5:00 commit our changes. 5:05 Enter a commit message of updated the default route, 5:10 and click the Commit All button. 5:15 I realize that it might feel like we took the long way around to fix our home page. 5:20 When doing software development, it's often helpful, not to mention important, 5:26 to understand the underlying mechanics first, 5:31 before making a change to your website. 5:34 Without a proper foundation, 5:37 it could be very easy to overlook or misunderstand the effects of a change, 5:38 even a simple change like the one that we did to fix our home page. 5:44 We have another change to make before our comic books list view is complete. 5:49 Allow the user to easily navigate from the list view to the detail view. 5:53 But before we do that let's do a short quiz to help solidify, 5:59 what you just learned about URL, routing and default routes. 6:03
You need to sign up for Treehouse in order to download course files.Sign up