微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

RazorEngine.NetCore:名称“ PopWriter”在当前上下文中不存在

如何解决RazorEngine.NetCore:名称“ PopWriter”在当前上下文中不存在

我陷入了RazorEngine.NetCore库的问题。我在netstandard2.0库中使用它,.NET Core应用程序和.NET 4.7应用程序(FullFramework)都在使用它。

对于某些模板,好的,它工作正常。但是对于某些模板,在编译模板时,它开始在生成的类内部使用某些方法,例如PushWriter和PopWriter。

IRazorEngineservice _razorService = RazorEngineservice.Create(new TemplateServiceConfiguration()
        {
            ReferenceResolver = new MyIReferenceResolver(),Debug = true,EncodedStringFactory = new RawStringFactory(),BaseTemplateType = typeof(CustomTemplateBase<>)
        });

然后,我从库(一个cshtml视图)中添加模板以及用作模型的类型:

_razorService.AddTemplate(name,ReadTemplate(template));
_razorService.Compile(name,type);

但是当它碰到Compile方法时,我得到了这个错误

Errors while compiling a Template.
Please try the following to solve the situation:
  * If the problem is about missing/invalid references or multiple defines either try to load 
    the missing references manually (in the compiling appdomain!) or
    Specify your references manually by providing your own IReferenceResolver implementation.
    See https://antaris.github.io/RazorEngine/ReferenceResolver.html for details.
    Currently all references have to be available as files!
  * If you get 'class' does not contain a deFinition for 'member': 
        try another modelType (for example 'null' to make the model dynamic).
        NOTE: You CANNOT use typeof(dynamic) to make the model dynamic!
    Or try to use static instead of anonymous/dynamic types.
More details about the error:
 - error: (111,16) The name 'PushWriter' does not exist in the current context
     - error: (115,258) The name 'PopWriter' does not exist in the current context
     - error: (131,16) The name 'PushWriter' does not exist in the current context
     - error: (138,41) The name 'PopWriter' does not exist in the current context
     - error: (150,16) The name 'PushWriter' does not exist in the current context
     - error: (152,277) The name 'PopWriter' does not exist in the current context
     - error: (200,16) The name 'PushWriter' does not exist in the current context
     - error: (202,261) The name 'PopWriter' does not exist in the current context
     - error: (413,16) The name 'PushWriter' does not exist in the current context
     - error: (415,224) The name 'PopWriter' does not exist in the current context
     - error: (468,16) The name 'PushWriter' does not exist in the current context
     - error: (470,277) The name 'PopWriter' does not exist in the current context
     - warning: (102,8) The variable 'sector' is assigned but its value is never used
Temporary files of the compilation can be found in (please delete the folder): C:\[folder-structure-hidden]\RazorEngine_kg4royrl.trc
The template we tried to compile is: 

我将Microsoft.AspNetCore.Mvc和Microsoft.AspNetCore.Mvc.Razor添加到项目引用中,也添加到了MyIReferenceResolver中,但是没有成功。

在Roslyn生成的异常中可以看到的部分生成代码是这样的:

BeginWriteAttribute("style"," style=\"",8783,"\"",8878,3);
            WriteAttributeValue("",8791,"min-width:100%;",15,true);
            WriteAttributeValue(" ",8806,new Microsoft.AspNetCore.Mvc.Razor.HelperResult(async(__razor_attribute_value_writer) => {
                PushWriter(__razor_attribute_value_writer);
                                                                                                                                                                                                if (f.something) {WriteLiteral("border-right: 1px solid #dddddd;");
                                                                                                                                                                                                                                                                    }PopWriter();
            }
            ),8807,70,false);

任何人都曾经经历过吗?

谢谢!

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。