RazorEngine - 命名空间导入编译错误

Rop*_*tah 6 .net razor razorengine

我在非MVC环境中使用Razor Engine(razorengine.codeplex.com).我编译存储在文件中的模板,@inherits用于智能感知支持.

  • RazorEngine大会
  • 自定义程序集 - 引用RazorEngine,包含View<>和设置View<>为基类
  • Web应用程序 - 引用RazorEngine,Custom Assembly,包含.cshtml模板文件

所有cshtml文件都有以下@inherits指令:

@inherits View<SomeModel>
Run Code Online (Sandbox Code Playgroud)

抛出错误:

找不到命名空间View的类型,是否缺少程序集引用?

我的web.config包含以下条目:

<add namespace="CustomAssembly.NamespaceContainingViewClass" />
Run Code Online (Sandbox Code Playgroud)

我认为这与其他条目有关<assemblies>,我CustomAssembly没有提到.是这样的吗?我可以使用另一个程序集中包含的自定义基类进行编译吗?

ps我无法检索程序集的强名称,因为我的自定义程序集引用了一个没有强名称的3d方程序集...

堆栈跟踪:

at RazorEngine.Compilation.DirectCompilerServiceBase.CompileType(TypeContext context)
at RazorEngine.Templating.TemplateService.CreateTemplate(String template, Type modelType)
at RazorEngine.Templating.TemplateService.GetTemplate(String template, Type modelType, String name)
at RazorEngine.Templating.TemplateService.Compile(String template, Type modelType, String name)
at RazorEngine.Razor.Compile(String template, Type modelType, String name)
Run Code Online (Sandbox Code Playgroud)

Pet*_*ter 7

您可以在TemplateServiceConfiguration中添加名称空间:

    TemplateServiceConfiguration templateConfig = new TemplateServiceConfiguration();
    templateConfig.Namespaces.Add("MyNamespaceGoesHere"); 
    templateConfig.Resolver = new DelegateTemplateResolver(name =>
    {
       <My template resolve implementation>
    }
    Razor.SetTemplateService(new TemplateService(templateConfig));
    using (TextWriter tw = new StringWriter())
    {
      Razor.Resolve(viewName + ".cshtml", model).Run(new ExecuteContext(), tw);
      var emailHtmlBody = tw.ToString();
    }
Run Code Online (Sandbox Code Playgroud)


Bui*_*ted 1

您可能需要将 razor 配置部分添加到您的web.config

<?xml version="1.0" encoding="UTF-8" ?>
<configuration>
    <configSections>
        <section name="razorEngine" type="RazorEngine.Configuration.RazorEngineConfigurationSection, RazorEngine" requirePermission="false" />
    </configSections>
</configuration>

<razorEngine>
    <namespaces>
        <add namespace="CustomAssembly.NamespaceContainingViewClass" />
    </namespaces>
</razorEngine>
Run Code Online (Sandbox Code Playgroud)

  • 我将其添加到包含视图的文件夹中:“/layout/razor_views/web.config”,但没有成功... (2认同)