RazorEngine import compilation error - Namespace

I am using the Razor Engine ( razorengine.codeplex.com ) in a non-MVC environment. I collect templates stored in files and use @inherits to support intellisense.

  • Razorengine assembly
  • Custom assembly - links RazorEngine contains View<> and sets View<> as a base class
  • Web Application - Links RazorEngine, Custom Assembly, contains .cshtml template files

All cshtml files have the following @inherits directive:

 @inherits View<SomeModel> 

An error is issued:

View namespace type not found, are you missing assembly reference?

My web.config contains the following entry:

 <add namespace="CustomAssembly.NamespaceContainingViewClass" /> 

I think this has something to do with another <assemblies> entry where my CustomAssembly not mentioned. This is true? Can I compile my own base class, which is contained in another assembly?

ps I can’t get a strong name for the assembly, because my custom assembly refers to the assembly of the 3d batch, which is not very 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) 
+4
source share
2 answers

You probably need to add a razor configuration section to 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> 
+1
source

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(); } 
+5
source

All Articles