Overview of URL Routing3:48 with James Churchill
In the previous video, we left the question as to how MVC associated the URL “ComicBooks/Detail” with our controller action unanswered. Let’s answer that question now.
In the previous video we left the question as to how MVC. 0:00 associated the URL comic books/detail with our controller action unanswered. 0:04 Let's answer that question now. 0:10 In the URL http://teamtreehouse.com/, 0:12 http is the protocol, and teamtreehouse.com is the host name. 0:17 Typically, the host name is the domain name of the website. 0:25 However, when running websites locally, a special name, localhost is used. 0:30 The number following the colon, after the host name, 0:36 is our port number, which in my case is 50153. 0:39 Your port number might be different as Visual Studio assigns a random 0:43 number when creating ASP.NET projects. 0:48 Using a different explicit port number for each project allows each to be run locally 0:51 at the same even though they're all using the same host name local host. 0:57 If the terms URL, Protocol, Host Name or 1:03 Port are unfamiliar to you, check the teachers notes for 1:08 links to resources that will explain these concepts in detail. 1:11 The URL, http://localhost:50153/ 1:16 would display the default page for our website. 1:22 Often this page is referred to as the Home Page. 1:27 For the URL, http://localhost:50153/ComicBooks/Detail, 1:31 everything to the right of the port number is known as the Path. 1:39 So while the combination of the host name and the port number 1:47 is used to determine which website the request is intended for, the path is used 1:51 to determine which resource within that website is being requested. 1:56 Whenever MVC processes the request, it uses simple pattern matching 2:01 against the path to determine the name of the controller and 2:05 action that should be used in the form of controller/action. 2:09 So the URL path, ComicBooks/Detail would map to a controller 2:14 named ComicBooks and an action method named Detail. 2:19 The process of matching a URL to a controller action is called url routing. 2:25 URL routing is not unique to Asp.net MVC. 2:31 Many other web frameworks, both server side and 2:36 client side, use the concept of URL routing. 2:39 Let's take a closer look at why we're getting an error 2:42 when requesting our website's home page. 2:46 When making a request, I'll supply in a path, 2:48 MVC will default the controller name to home and the action name to index. 2:51 We're getting a 404 error because our project doesn't contain a controller 2:56 named home controller, let alone a method on that controller named index. 3:02 If we were to request the URL path comic books, MVC would successfully find 3:06 our comic books controller but we'd still get a 404 error because our 3:12 controller currently doesn't contain an action method named index. 3:17 In a later video, 3:21 we'll add the index action method which will display a list of comic books. 3:23 We're just scratching the surface of what URL routing is capable of. 3:28 But for now, these basic concepts are just what we need to know 3:32 in order to continue building out our website. 3:36 Our controller's detail action method currently just returns a string literal. 3:39 Let's see if we can improve upon that design in the next video. 3:44
You need to sign up for Treehouse in order to download course files.Sign up