Debugging ASP.NET MVC 2 Source Code

I’m trying to write some templates for MVC 2 and one of the most useful things I’ve found is being able to step into the source code and see how it is actually interpreting what you’ve written.

It’s easy enough to acquire the source, but what you might not have done before is set up a symbol server. A symbol server is a location that the Windows and Visual Studio debugging tools can use to obtain pdb files, so that you can debug almost anything, including drivers and the operating system!

Setup Symbol Server

  1. Create a directory, preferably in a shared location if there’s more than one of you as this stuff can get quite big.
  2. Change the directory to allow compression as pdb’s will typically compress by > 50%.
  3. Add a new system environment variable _NT_SYMBOL_PATH and set it to SRV****http://referencesource.microsoft.com/symbols;http://msdl.microsoft.com/download/symbols

Enable Debug Symbols in Visual Studio

  1. Install the debugging tools for Windows and run Start Menu|Programs|Microsoft Windows SDK 7.1|Visual Studio Registration|Windows SDK Configuration Tool; this allows Visual Studio to use the symbols.
  2. Run Visual Studio and go to Tools -> Option -> Debugging -> General
  3. Uncheck Enable Just My Code
  4. Check Enable .NET Framework source stepping
  5. Change to Symbols
  6. Update “Cache symbols from symbol servers to this directory” to point at your shared symbol directory, then hit “Load symbols from Microsoft symbol servers”, this will take a while and chew up about 111Mb (~48Mb compressed)
  7. Put a break point in one of you controllers and run through to that point.
  8. Pick a stack frame from System.Web.Mvc from Call Stack and select Load Symbols if it is not disabled and then Go To Source Code, picking the location you have put MVC 2

One other interesting angle on this is Symbol Source who are providing symbol/source service for a bunch of open source projects including NHibernate, Castle and MVC Contrib