c# - Razor runco​​mpile 不允许我调试

标签 c# razor razor-2 razorengine

我使用的是 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 贡献者

关于c# - Razor runco​​mpile 不允许我调试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28272371/

相关文章:

c# - 以 pdf 格式输出 С# iTextSharp

c# - 使用 Html.Raw 将 ASP.NET 模型序列化为 JSON 时出现 Razor 语法错误

c# - 在 .NET 中验证时 '/' 中的服务器错误

asp.net-mvc-4 - 如何在ASP.Net MVC 4中为特定区域定义布局?

javascript - 如何通过复选框值过滤表

c# - 在自托管的 OWIN Web API 中,如何在关机时运行代码?

c# - Windows 身份验证不接受凭据

.net - MVC 4 中带有 MVC 4 Futures 的强类型 ActionLink 的语法是什么?

c# - EF 查询特定表查询 db,ModelStoreConatiner 名称为 Schema

c# - 在 Razor Helper 中使用 @section