Razor runco​​mpile 不允许我调试

Razor runcompile is not allowing me to debug

我使用的是 razor 的最新版本。 https://github.com/Antaris/RazorEngine

我想将它附加到一些 cshtml 并针对它进行调试。

自述文件说明如下

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);

现在我已经在该路径创建了一个包含以下内容的 cshtml 文件。

@{
     var a = 1;
     a = a + a;     
     @a    
}

<div>
     hi
</div>

但是我得到一个空字符串:( 当我按 f11 进入它时,它只是跨过 :( :(.

我不确定我做错了什么任何人有任何想法。


回复码

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);

LoadedTemplateSource代表模板源码,你给的是""作为源码,所以你的模板是空的。

LoadedTemplateSource第一个参数需要是模板源代码,第二个参数是文件路径,仅供调试使用

如果您需要延迟加载或自定义加载程序策略,您可以实施自定义 ITemplateSourceITemplateManager,但是让源始终在内存中可用也可以改善一些错误消息。


matthid,RazorEngine 贡献者

选项 > 调试 > [=12]中禁用“只启用我的代码” =]一般 为我工作。