Search code examples
.netrazorrazorengine

RazorEngine - Namespace import compilation error


I'm using the Razor Engine (razorengine.codeplex.com) in a non-MVC environment. I compile templates that are stored in files and use @inherits for intellisense support.

  • RazorEngine Assembly
  • Custom Assembly - references RazorEngine, contains View<> and sets View<> as baseclass
  • Web application - references RazorEngine, Custom Assembly, contains .cshtml template files

All cshtml files have the following @inherits directive:

@inherits View<SomeModel>

An error is thrown:

The type of namespace View isn't found, are you missing an assembly reference?

My web.config contains the following entry:

<add namespace="CustomAssembly.NamespaceContainingViewClass" />

I think this has something to do with the other entry <assemblies>, where my CustomAssembly isn't mentioned. Is this the case? Can I compile with my custom base class which is contained in another assembly?

p.s. I cannot retrieve a strong name for the assembly because my custom assembly references a 3d party assembly which isn't strongly named either...

Stacktrace:

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)

Solution

  • You probably need to add the razor config section to your 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>