Overview of URL Routing6:02 with James Churchill
Let's take a closer look at the URLs for the pages in our website.
For more information about how the internet works or HTTP, be sure to check out these Treehouse courses.
If you'd like to learn more about ASP.NET MVC Routing, check out this tutorial on the official ASP.NET website.
Before we move on, let's recap what we've covered so far. 0:00 We created an NBC website using Visual Studio. 0:04 Once we created our website, we learned how to run it so 0:08 that we could preview it in a browser. 0:11 Then we looked at how our project is put together. 0:13 We've covered a lot of ground. 0:16 Give yourself a part on the back. 0:18 Now let's build upon that base by learning how to add a new page to our website. 0:20 But before we do that let's take a bit of a tangent and 0:25 take a closer look at the U.R.L.'s for the existing pages on our website. 0:28 Chrome by default won't display the full U.R.L. in the address bar. 0:33 So I'm going to select Internet Explorer for my browser, 0:37 then click the play button, or the FI function key to start running our website. 0:41 Sometimes Visual Studio won't bring the selected browser to the foreground. 0:47 So if you're not seeing your browser, go ahead and bring it to the foreground. 0:51 Looking in the address bar, our current URL is HTTP 0:54 colon slash slash local host colon 50356 slash. 1:00 Let's break this down in URL. 1:05 HTTP is the protocol and local host is our host name. 1:09 Typically, the host name is the domain name of the website. 1:13 So for example www.teamtreehouse.com or www.google.com. 1:16 Since we're running our website locally the special name local host is used. 1:21 The number following the colon after the host's name is our port number, 1:27 which in my case is 50356. 1:31 Your report number might be different as Visual Studio assigns a random number 1:35 when creating ASP.NET projects. 1:39 Using a different explicit port number allows each project to be ran locally 1:42 at the same time even though they're all using the same host name local host. 1:47 If the terms URL, protocol, hostname, or 1:52 port are unfamiliar to you check the teacher's notes for 1:55 links to resources that will explain these concepts in detail. 1:58 You'll notice that as we browse around our website the protocol, hostname and 2:02 port number won't change. 2:06 Given that, as we talk further about URLs I'm only gonna focus on the part of 2:08 the URL to the right of the port number, which is often referred to as the path. 2:12 If we click on the about menu item to browse to the about page 2:18 the URL changes to home slash about. 2:22 And if we click the contact menu item to browse to the a contact page the URL path 2:26 changes to home/contact when browsing to pages in our website the N.P.C. 2:31 framework will look for a controller and action to handle that request. 2:36 So how does this work whenever in V.C. process is a request for a U.R.L., 2:41 it uses simple pattern matching to determine the name of the controller and 2:47 action in the form of controller slash action. 2:51 So the URL path home/about would map to a controller named home and 2:55 an action named about. 3:00 And home/contact would map to a controller named home and an action named contact. 3:02 Go ahead and switch back to Visual Studio and 3:09 click the stop button to stop running our website. 3:12 Now let's see if we can find the Home Controller in the Solution Explorer panel. 3:15 Remember, all of our controllers are located in the controllers folder. 3:20 If the folder isn't expanded, double-click the folder name to display its contents. 3:24 Here's a file named HomeController.cs. 3:30 That looks promising. 3:32 Go ahead and double-click it to open it. 3:34 Here's our home controller. 3:37 We can see that the home controller class has three methods index, about, 3:39 and if we scroll down a little bit, contact. 3:44 That's interesting. 3:47 The about and contact methods match the action names for our about and 3:50 contact pages. 3:54 That's not a coincidence. 3:55 These methods are special methods called action methods. 3:57 When we browse to the about or contact pages, the NPC framework will find 4:01 the home controller and call the about method if we're viewing the about 4:05 page or the contact method if we're viewing the contact page. 4:10 So what about the index method? 4:15 We don't have an index page on our website. 4:19 So when would this method get called? 4:21 To answer this question, let's start our website again and look at the URL for 4:23 the homepage. 4:28 The URL is http://localhost, 4:28 followed by our port number, which doesn't include a path. 4:33 So if the control or action name is not supplied in the URL path, 4:38 how does NPC know which controller and action to use? 4:42 When we make requests for 4:45 pages on our website, it turns out we don't have to supply an action name. 4:46 When we don't, index is used by default, so browsing to the URL home 4:51 would result in the home controllers index method being called. 4:56 We also don't need to provide a controller name. 5:00 If we don't the home is used by default. 5:03 This allows us to browse to our home page using nothing but our host name, and 5:05 in our case the port number. 5:10 So what happens if we try to browse to a page that 5:13 doesn't have a matching control or action. 5:15 Let's try it and see. 5:18 In the address bar, let's type the URL home/new and 5:19 press the enter key.Ah we get an error page. 5:25 Specifically the error message the resource cannot be found. 5:30 Let's go ahead and change new to about and press the enter key. 5:34 Now we're back at our about page. 5:42 In ASP.NET NVC, the process of matching no URL to a controller an action 5:45 is called URL routing. 5:50 We're just scratching the surface of what URL routing is capable, but for 5:52 now, these basic concepts are just what we need to know, 5:56 in order to add a new page to our website. 5:59
You need to sign up for Treehouse in order to download course files.Sign up