Razor runcompile不允许我调试

太空人

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

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

自述文件指出以下内容

调试

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

config.Debug = true;

当Debug为true时,您可以将debug直接整理为生成的代码。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>

但是我返回了一个空字符串:(当我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贡献者

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章