Validating a Bug Fix with the Debugger5:23 with James Churchill
Let’s review the solution to our error and how to use the Visual Studio debugger to validate our work.
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
See these MSDN pages for more information on how to use the Visual Studio debugger.
F11- Step Into
F10- Step Over
SHIFT+F11- Step Out
Were you able to resolve the issue? 0:00 Let's walk through the solution now. 0:02 So it turns out we're making a simple mistake. 0:04 We aren't setting the Id property on our model instances. 0:08 To fix the problem let's set the Id property values to 1,2, and 0:12 3 respectively for our model instances. 0:17 Restart the website and browse to /ComicBooks/ Detail/1. 0:23 Okay, we're at our break point again just inside the detail action method. 0:30 Just like last time, we could use F10 and 0:35 F11 to step through our code until we get to the get comic book methods for 0:38 each loop or we could set another breakpoint on that line of code. 0:43 To save us a little time, let's set another breakpoint. 0:48 Switch to the comic book repository class and set a breakpoint on the line of code 0:51 that contains the if statement inside of the for each loop. 0:56 Now we can press F5 to continue execution up to that breakpoint. 0:59 This time in the Autos window, we can see that the comicBook Id property value is 1. 1:05 Press F10 twice to step down to the comic book to return variable assignment. 1:10 Press F10 again to move to the break statement, 1:17 which will break us out of the loop when we press F10 one more time. 1:20 Isn't it cool to see the benefits of short circuiting the loop 1:24 as we step through the code? 1:28 Press Shift+F11 to step out of the method and return to the detail method, 1:30 then press F10 to step down to the return statement. 1:35 Now we can see that the comicBook variable is set to our first comic book, 1:39 The Amazing Spider-Man #700. 1:43 Press F5 to resume execution, and here's our comic book detail page. 1:46 While we're here, let's go ahead and browse to the other two comic books. 1:52 Just change the 1 in the URL to 2 and press Enter. 1:56 We're at our breakpoint again. 2:01 Press F5 to continue execution to our second break point. 2:03 We can see that the id argument value is 2 and the comicBook.Id property value is 1. 2:07 That should cause our if statement to evaluate to false. 2:14 Press F10 to step to the next statement. 2:17 Sure enough the if statement evaluated to false as we expected. 2:20 Press F5 to continue to our breakpoint again. 2:25 Now our comicBook.Id property value is 2, which matches our ID argument value. 2:27 Press F10 twice to see execution fall into the if statement. 2:34 We found the comic book that we're looking for. 2:39 Press F5 to continue execution. 2:41 And here's the comic book detail page showing the information for 2:45 the Amazing Spider Man number 657. 2:49 Now change the 2 in the URL to 3 and press Enter. 2:52 We're at our first breakpoint again. 2:57 This time let's disable all of our breakpoints before we continue execution. 2:59 Click on the Breakpoints tab here at the bottom to show the Breakpoints window. 3:04 Then click on the check boxes here and here to disable our breakpoints. 3:08 Notice how the little red circles are no longer filled in. 3:14 That's how Visual Studio officially indicates that a breakpoint is disabled. 3:18 Press F5 to continue execution. 3:22 And here's our third comic book. 3:26 Let's close Chrome and stop our website. 3:28 If you're using GitHub, let's commit our changes. 3:32 Enter a commit message of Updated comic books controller to use repository. 3:35 And click the Commit All button. 3:44 Let's also sync with the remote server. 3:46 Navigate to the synchronization panel and click the push link. 3:49 >> The Visual Studio debugger offers a great set of tools for 3:55 debugging errors and helping you get more familiar with your code. 3:58 Experienced developers often don't consider a coding task completed until 4:03 they've stepped through their code line by line, validating its runtime behavior. 4:08 As we were debugging you probably noticed that we did a lot of switching back and 4:14 forth between Visual Studio and our browser. 4:19 It's not uncommon for 4:22 developers to have at least two monitors, which allows Visual Studio and 4:23 the browser to be displayed at the same time, one on each monitor. 4:28 We're just scratching the surface of what can be done with the debugger. 4:33 See the teachers notes for links to additional resources. 4:36 Let's do a quick review of the section. 4:40 We improved the structure of our [SOUND] comic book data by adding our first data 4:42 models. 4:47 We learned how [SOUND] strongly typed views can be used to eliminate any 4:47 reliance on the view bag property. 4:52 [SOUND] We applied software design principles and the [SOUND] repository 4:54 design pattern to improve the overall code quality of our website. 4:59 And we saw how to use the Visual Studio debugger to troubleshoot an error. 5:03 We've been busy, great job sticking with it. 5:10 In the next section, we'll work on adding a comic book's list page to our website. 5:13 I'm looking forward to finally having a homepage. 5:18 See you then. 5:22
You need to sign up for Treehouse in order to download course files.Sign up