我使用的是 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
的第一个参数需要是模板的源代码,第二个参数是文件的路径,仅用于调试目的。
如果您需要延迟加载或自定义加载器策略,您可以实现自定义 ITemplateSource
或 ITemplateManager
,但是让源始终在内存中可用会改善一些错误消息
matthid,RazorEngine 贡献者
关于c# - Razor runcompile 不允许我调试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28272371/