Debugging Razor templates

Can you allow the in-memory class generated by the RazorPageHost to be compiled with debug information included (and without “/optimize” if necessary).

var @params = new CompilerParameters
{
	GenerateInMemory = true,
	GenerateExecutable = false,
	IncludeDebugInformation = true,
	CompilerOptions = "/target:library /optimize",
	TempFiles = { KeepFiles = true }
};

This would at least give a useful stack trace if an error occurs while rendering a razor template.

At present, it’s very tedious and time consuming to find where the error came from (especially for NullReferenceExceptions).

CompilerParameters.IncludeDebugInformation can be enabled either by HostConfig.DebugMode setting or by a new config setting on RazorFormat.

I have already added this comment to the uservoice request: https://servicestack.uservoice.com/forums/176786-feature-requests/suggestions/8964094-allow-debuggable-razor-views.

RazorFormat.IncludeDebugInformation and RazorFormat.CompileFilter are new options added in this commit that will let you modify the Razor CompilerParameters.

By default it will include DebugInfo in DebugMode.

This change is available from v4.0.45 that’s now available on MyGet.

Great, thanks.

How much work would it be to implement full razor debugging in VS?

What do you mean by full razor debugging? Have you tried debugging Razor Views in DebugMode?

Oh, I didn’t realise that’s all there is to it.

Just tested, it works perfectly.

The only tweak I would make is to remove the “/optimize” by default from the CompilerOptions if IncludeDebugInformation is true.

Without this, you can step through the razor code in the debugger, but you won’t have access to the local variables (the debugger says “Cannot evaluate expression because the code of the current method is optimized”).

I know it can already be done using the new CompileFilter on RazorFormat, but I think it’s a more sensible default, so razor can be easily debugged if DebugMode is on, without needing to override the compiler options.

Yep Agreed, added in this commit.

It’s now available on MyGet, since you have v4.0.45 installed you’ll need to clear your NuGet cache.