Updating the Controller7:16 with James Churchill
Now that we have our comic books repository, let’s update our controller to use it.
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/v4.7 -b updating-the-controller
CTRL+COMMA- Opens the “Navigate To” dialog
CTRL+PERIOD- Opens the quick actions popup
Now that we have our comic books repository, 0:00 we can update our controller to use it. 0:03 Let's open the comic books controller class. 0:05 We could use the solution explore to find the file that we're looking for, but 0:08 let's look at another option. 0:12 Pressing control comma will open Visual Studios, navigate to dialog. 0:14 Now we can start typing the class, method, or property name that we're looking for. 0:19 So in this case, I'll type comic. 0:24 Visual Studio will search across the project and show the matching items. 0:28 I'll use the down arrow key to select the fifth item in the list, 0:32 the comic books controller class, and then press Enter to open the file. 0:36 The navigate to dialogue is a great way to open files 0:41 without having to take your hands off the keyboard. 0:45 Let's add a private field for our repository. 0:48 I'll start by typing private ComicBookRepository. 0:52 Notice that Visual Studio is already letting us know 1:00 that something is wrong with our type name. 1:03 The red squiggle is a dead giveaway. 1:05 If we leave the cursor right at the end of ComicBookRepository, 1:08 we can press control period to open the list of suggested quick actions, 1:12 instead of having to use the mouse to click on the light bulb icon. 1:17 The first item in the list to add the missing using directive 1:21 is what we want to do. 1:25 So we can just press Enter to perform this action. 1:26 Then, we can continue with adding our field. 1:30 Let's name the field _comicBookRepository. 1:33 Notice that I'm starting the field name with an underscore and 1:39 lower case letter, which is a common naming convention for private fields. 1:43 Then set the field value to null. 1:47 I'll collapse the solution explorer panel to give us more room. 1:50 Now, we need an instance of our repository. 1:54 Let's add a constructor and instantiate a repository within it. 1:57 Remember, that constructors are special methods 2:03 that are called when an instance of our class is being instantiated. 2:06 We use constructors to initialize instance members, 2:10 which is exactly what we're doing with our repository field. 2:13 Constructors can be identified by their lack of a return type. 2:17 And their name, which matches the class name. 2:21 Next, let's update the detail method. 2:24 To start, add an ID parameter of type int. 2:28 Then, let's replace the inline instantiation of the comic book model 2:32 with a call to the repository's get comic book method. 2:36 Be sure to pass the ID argument into the method call. 2:41 Ready to preview our changes? 2:44 Save the controller and run the website. 2:46 I don't know about you, but I'm getting tired of seeing our home page 404. 2:51 I promise we'll fix that soon. 2:55 But for now, go ahead and browse. 2:58 The /comic_books/detail and we got an error. 3:00 The parameters dictionary contains a no entry for 3:06 parameter ID of non noble type system.in32 for 3:09 method system web NVC Action Results detail in 32. 3:14 In comic book gallery, controllers, comic books controller. 3:21 An optional parameter must be a reference type, a null-able type. 3:25 Or be declared as an optional parameter. 3:29 Let's break this down. 3:32 We can see here and here that this 3:33 error message refers to our comic books controller detail action method. 3:36 Remember how we added an ID parameter to our detail action method. 3:41 Well, this error message is telling us 3:45 that the request didn't include a value for the ID parameter. 3:48 So in Mvc was unable to call our action method. 3:52 Let's take another look at how Mvc URL routing 3:57 associates URL's with controller action methods. 4:00 In a previous video, we discussed that MVC uses simple 4:04 pattern matching against the URL path to determine the name of the Controller and 4:08 Action should be used in the form of controller/action. 4:13 So, the URL path ComicBooks/Detail would map to a controller 4:18 named ComicBooks and an action method named Detail. 4:24 As it turns out, the actual pattern that MVC uses is controller/action/id. 4:29 The third part of that pattern, the ID segment is optional. 4:37 This means that MVC can successfully match the pattern against a URL, 4:41 even if the path doesn't include the ID segment. 4:47 Unfortunately, MVC is getting confused about what to do with our request for 4:50 /ComicBooks/Detail. 4:55 The problem is that even though the ID parameter should be optional, 4:58 our detail action method only accept an integer which isn't nullable. 5:03 So, how do we resolve this issue? 5:09 It turns out that C# provides us with an easy solution so 5:11 that we can make the necessary changes to our detail action method. 5:15 If we update our parameter type to be nullable by placing a question mark 5:20 after the keyword int, then MVC can successfully pass null for 5:24 the ID parameter if an ID value isn't provided as part of the request. 5:29 Nullable types will be covered in more detail in future courses. 5:34 Now, we need to do a bit of defensive coding 5:39 by checking to see if the ID argument is null. 5:42 And if so, we turn a not found error to the client. 5:45 We can take advantage of the base controllers http not found method 5:50 to return an http not found result object. 5:54 Lastly, notice that visual studio is unhappy about passing 6:00 the ideal argument to the repositories GetComicBook method. 6:04 Argument one cannot convert from question mark to int. 6:08 When using a nullable type, 6:14 you need to use the value property to get at the underlying value. 6:16 You could also use an explicit cast. 6:21 Use whichever one feels the most readable or natural to you or 6:24 whichever one your development team has decided is their preference and 6:29 do that consistently throughout the project. 6:33 Restart the website, so that we can retest the /comicbooks/detail route. 6:36 Don't forget to manually browse to the correct route, great. 6:43 I've never been so happy to see a 404 error. 6:47 Let's change our URL, so that it includes the optional ID parameter. 6:51 Bomber we just encountered an unhandled exception in our detailView 6:56 Click OK to dismiss the exception dialog. 7:02 Hm, I wonder what we did wrong. 7:06 In the next video, we'll walk through how to use the Visual Studio debugger 7:08 to troubleshoot this error. 7:12 See you then. 7:14
You need to sign up for Treehouse in order to download course files.Sign up