Skip to content
Alexander Selishchev edited this page Oct 22, 2026 · 6 revisions

Templates can be debugged in Visual Studio.

  1. 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.

  1. 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().

  1. Use @{ Breakpoint(); } to stop at a particular line

Breakpoint() does nothing unless EnableDebugging() was called, so it is safe to leave it in templates.

  1. When you run the code, VS will ask you for the cshtml file location image

This will be either your bin folder (current working directory) or the path specified in EnableDebugging("MyTempPath")

Clone this wiki locally