How to Debug and ’Patch’ ASP.NET Core MVC Source Code

Joni 【ジョニー】
4 min readFeb 27, 2018

--

In this post, I will show you how to debug and possibly edit ASP.NET Core MVC source code, and test your ‘patches.’

In fact, you can apply this knowledge to debug any external libraries that you are referencing from your *.sln, given you have access to the source code. What? Confuse?

Imagine two different teams in the same company work on a different project, different *.sln. Team A build the MVC project. Team B build the wrapper ShinyPants.dll, to be used by the Team A. So, the MVC project has a reference to ShinyPants.dll.

Now that something bad happened. Team B said that their code is perfectly fine. So team A was asked by their manager to start the investigation. But team A found nothing wrong with their code. They need an evidence to blame that it is that ShinyPants.dll doing something bad. Since they are in the same company, Team A is allowed to access the source code of Team B.

So if you are in this situation, you can apply this technique to debug that kind of external library right within your solution.

Stop talking. No, I am not talking. I am writing. Here we go.
Forget Team B’s ShinyPants.dll for now. Push that ShinyPants to our reading stack.

To debug ASP.NET MVC code, I wrote about it in the past and I respect
DRY principle
, so please refer to my previous post here:
https://medium.com/@joni2nja/friends-dont-let-friends-swallow-nullreferenceexception-acd949833d70

Next.
Clone ASP.NET MVC source code from https://github.com/aspnet/mvc, then switch to the appropriate branch, for example, release/2.0.0.

Then let’s try to edit this file:
https://github.com/aspnet/Mvc/blob/release/2.0.0/src/Microsoft.AspNetCore.Mvc.ViewFeatures/Internal/ExpressionMetadataProvider.cs#L87
to something like this:

Then build it. Make sure it is using Debug configuration:

Debug configuration.

Now, go to the build output directory:

..\Microsoft.AspNetCore.Mvc.ViewFeatures\bin\Debug\netstandard2.0

You should see something like this:

Copy Microsoft.AspNetCore.Mvc.ViewFeatures.dlland Microsoft.AspNetCore.Mvc.ViewFeatures.pdbto ASP.NET MVC framework store directory:

C:\Program Files\dotnet\store\x64\netcoreapp2.0\microsoft.aspnetcore.mvc.viewfeatures\2.0.2\lib\netstandard2.0

Make sure to backup the original file.

Now if I run the project from my previous post:

Our ‘patch’!

But how can we set breakpoint to debug?

Simply open the file we modified right from the project we are debugging now.

Now reload the page from the browser.

That simple.
The secret is the *.PDB file, Pants Data Base 🙊

See Jon Skeet’s answer here about PDB: https://stackoverflow.com/questions/3899573/what-is-a-pdb-file/3899586#3899586

Forget Team B’s ShinyPants.dll for now.

Now, let’s pop that ShinyPants from our reading stack.

You should now understand how to debug it. Yes, compile it using Debug build, copy the output *.dll and *.pdb to your project output directory. Open the source file and set the breakpoint.
That’s it.

PRO TIP:

If you know the function name, try this:

Then type the function name:

When you access the page, the debugger will just stop.

(Model property is get_Model in IL level)

--

--