I'm using the latest version of razor from. https://github.com/Antaris/RazorEngine
I want to attached it to some cshtml and debug against it.
The readme states the following
Debugging
One thing you might want to enable is the debugging feature:
config.Debug = true;
When Debug is true you can straight up debug into the generated code. RazorEngine also supports debugging directly into the template files (normally .cshtml files). As as you might see in the above code there is no file to debug into. To provide RazorEngine with the necessary information you need to tell where the file can be found:
string template = "Hello @Model.Name, welcome to RazorEngine!"; string templateFile = "C:/mytemplate.cshtml" var result = Engine.Razor.RunCompile(new LoadedTemplateSource(template, templateFile), "templateKey", null, new {
In my code i have setup the following
var config = new TemplateServiceConfiguration(); // .. configure your instance config.Debug = true; var service = RazorEngineService.Create(config); Engine.Razor = service; //string template = "Hello @Model.Name, welcome to RazorEngine!"; string templateFile = "C:/mytemplate.cshtml"; var template = new LoadedTemplateSource("", templateFile); var result = Engine.Razor.RunCompile(template, this.Name, null, model);
Now I have created a cshtml file at that path with the following in it.
@{
var a = 1;
a = a + a;
@a
}
<div>
hi
</div>
But I get returned an empty string :( And when i f11 into it it just steps over :( :(.
Im not sure what im doing wrong anyone got any ideas.
Answer code
string templateFile = "C:/mytemplate.cshtml";
StringBuilder sb = new StringBuilder();
using (StreamReader sr = new StreamReader(templateFile))
{
String line;
// Read and display lines from the file until the end of
// the file is reached.
while ((line = sr.ReadLine()) != null)
{
sb.AppendLine(line);
}
}
string allines = sb.ToString();
var template = new LoadedTemplateSource(allines, templateFile);
var result = Engine.Razor.RunCompile(template, this.Name, null, model);
The LoadedTemplateSource
represents the template source code, and you have given ""
as the source code, therefore your template is empty.
The first parameter of LoadedTemplateSource
needs to be the source code of the template and the second one is the path to the file, which is only used for debugging purposes.
If you need lazy loading or a custom loader strategy you can either implement a custom ITemplateSource
or ITemplateManager
, however having the source available in memory all time improves some error messages as well.
matthid, a RazorEngine Contributor
Disabling "Enable Just My Code" in Options > Debugging > General worked for me.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With