Repository navigation
Debugging
Templates can be debugged in Visual Studio.
- Compile the template with the
IncludeDebuggingInfo()option. It will produce and keep the PDB and the template source code.
IRazorEngineCompiledTemplate template2 = razorEngine.Compile(templateText, builder =>
{
builder.IncludeDebuggingInfo();
});Starting with 2026.10.1, templates compiled with IncludeDebuggingInfo() are built without optimizations (OptimizationLevel.Debug), so local variables can be inspected in Watch / Locals windows and stepping follows template lines. Such templates run slower — do not use this option in production.
Debugging info survives SaveToFile() / LoadFromFile() (and stream variants), so a saved template can be debugged after loading as well.
- Call
EnableDebugging()before running the template. It will write the template source code to the current working directory, enabling VS to step into the template.
template2.EnableDebugging(); // optional path to output directory
string result = template2.Run(new
{
Title = "Welcome"
});EnableDebugging() throws RazorEngineException if the template was compiled without IncludeDebuggingInfo().
- Use
@{ Breakpoint(); }to stop at a particular line
Breakpoint() does nothing unless EnableDebugging() was called, so it is safe to leave it in templates.
- When you run the code, VS will ask you for the cshtml file location
This will be either your bin folder (current working directory) or the path specified in EnableDebugging("MyTempPath")