Razor runco​​mpile不允许我调试

时间:2015-02-02 06:44:58

标签: c# razor razor-2 razorengine

我正在使用最新版本的剃须刀。 https://github.com/Antaris/RazorEngine

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

自述文件陈述以下内容

  

调试

     

您可能想要启用的一件事是调试功能:

config.Debug = true;
     

当Debug为true时,您可以直接调试生成的代码。 RazorEngine还支持直接调试   模板文件(通常是.cshtml文件)。正如你可能在中看到的那样   上面的代码没有要调试的文件。提供RazorEngine   您需要告知文件所在位置的必要信息   实测值:

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 { 
     

在我的代码中,我设置了以下内容

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>

但我收到一个空字符串:( 当我进入它时它只是跨过:( :(。

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


答案代码

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

2 个答案:

答案 0 :(得分:8)

LoadedTemplateSource代表模板源代码,您已将""作为源代码,因此您的模板为空。

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

如果您需要延迟加载或自定义加载器策略,您可以实现自定义ITemplateSourceITemplateManager,但是在内存中始终提供源可以改进一些错误消息。


matthid,RazorEngine贡献者

答案 1 :(得分:2)

选项&gt;中停用“启用我的代码调试&gt; 将军为我工作。

相关问题