Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Razor runcompile is not allowing me to debug

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);
like image 772
Spaceman Avatar asked Dec 14 '22 16:12

Spaceman


2 Answers

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

like image 65
matthid Avatar answered Dec 21 '22 23:12

matthid


Disabling "Enable Just My Code" in Options > Debugging > General worked for me.

like image 39
JJS Avatar answered Dec 21 '22 23:12

JJS