How to debug your application with MVC fresh source code

Before this post, some guys were wrote some articles about this. But when I actually did this, I found those articles are out of date. Now I am sharing my experience to you guys:

Step 1: Download the newest MVC source code 

You can access https://aspnetwebstack.codeplex.com/SourceControl/latest# to get the newest code in a zip file. After you downloaded file, please unzip entire solution in a folder.

Step 2: Remove strong name from each project

Before each version of MVC launch, the contribute team always strong name each MVC related assembly by a specified keyfile 35MSSharedLib1024.snk which is located in tools folder to prevent assembly tamper. But the snk file that you get doesn't contain private key, that you can only delay signed all assemblies if you compile directly. Unfortunately, delay signed assembly doesn't support debug feature. So you have to disable assembly strong name at project properties one by one.

 Step 3: Change assebmly reference in System.Web.WebPages project

In the System.Web.WebPages project, some kinds of type must have permissions to access private members of System.Web.Mvc.dll and System.Web.Helpers.dll. In AssemblyInfo.cs file of System.Web.WebPages, You will see these lines:

 [assembly: InternalsVisibleTo("System.Web.Mvc, PublicKey=0024000004800000940000000602000000240000525341310004000001000100b5fc90e7027f67871e773a8fde8938c81dd402ba65b9201d60593e96c492651e889cc13f1415ebb53fac1131ae0bd333c5ee6021672d9718ea31a8aebd0da0072f25d87dba6fc90ffd598ed4da35e44c398c454307e8e33b8426143daec9f596836f97c8f74750e5975c64e2189f45def46b2a2b1247adc3652bf5c308055da9")] 
 [assembly: InternalsVisibleTo("System.Web.Helpers, PublicKey=0024000004800000940000000602000000240000525341310004000001000100b5fc90e7027f67871e773a8fde8938c81dd402ba65b9201d60593e96c492651e889cc13f1415ebb53fac1131ae0bd333c5ee6021672d9718ea31a8aebd0da0072f25d87dba6fc90ffd598ed4da35e44c398c454307e8e33b8426143daec9f596836f97c8f74750e5975c64e2189f45def46b2a2b1247adc3652bf5c308055da9")]

You should replace it with this two lines:

 [assembly: InternalsVisibleTo("System.Web.Mvc")] 
 [assembly: InternalsVisibleTo("System.Web.Helpers")]

 And then you should remove WebApiHelpPageVB project from solution.

 By now you can compile entire solution without strong name successfully. ;)

Step 4: Add your application into MVC solution

After you add your MVC application by "Add Existing Project", you have to change a lots of assembly reference in your application. Here is the list:

Microsoft.Web.Infrastructure.dll
Microsoft.Web.Mvc.dll
System.Web.Mvc.dll
System.Web.Razor.dll
System.Web.WebPages.Deployment.dll
System.Web.WebPages.dll
System.Web.WebPages.Razor.dll

You have to delete old reference and add these assemblies in Bin\Debug folder that you just compiled.

 And then you have to change assembly reference in web.config files located in your application folder and Views folder. You have to remove version and public key information from element attributes like this:

Before:

 <sectionGroup name="system.web.webPages.razor" type="System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"> 
 <section name="host" type="System.Web.WebPages.Razor.Configuration.HostSection, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" /> <
 section name="pages" type="System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" /> 
 </sectionGroup>

 

After:

  <sectionGroup name="system.web.webPages.razor" type="System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, System.Web.WebPages.Razor">
 <section name="host" type="System.Web.WebPages.Razor.Configuration.HostSection, System.Web.WebPages.Razor" requirePermission="false" />
 <section name="pages" type="System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor" requirePermission="false" />
 </sectionGroup> 

 Certainly, you also have to comment <assemblybinding> elements in your config file.

Finally, you have to upgrade third-party project by NuGet especially WebGrease. In official release MVC version, the referenced version of Web Grease is v1.5. The newest MVC references WebGrease v1.6.  

 

To dig in MVC source code is a better way to know how MVC works. Here is a good reference: https://www.asp.net/mvc/tutorials/mvc-5/lifecycle-of-an-aspnet-mvc-5-application