Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)
like image 470
Ropstah Avatar asked May 16 '11 11:05

Ropstah


1 Answers

You can add a namespace in the 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();
    }
like image 101
Peter Avatar answered Sep 22 '22 00:09

Peter