RazorEngine - Namespace import compilation error
Asked Answered
P

2

6

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)
Petitioner answered 16/5, 2011 at 11:59 Comment(4)
Where is this error thrown? What's the stack trace?Brigand
The error is thrown when I call Razor.Compile with arguments (one of which is the templatestring containing @inherits). I've added the stacktrace to the question above.Petitioner
Does it work when you have the fully qualified name after @inherits. It might be that MVC does some extra work to get the namespaces to work. Also did you try and add the namespace in the template itself with @using MyNamespace;Pancreatotomy
Yes it does work with full name. I personally think that the <assemblies> entry needs to be provided also however this is not possible due to strong naming. I have no publickeytoken to provide...Petitioner
B
1

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>
Baldpate answered 16/5, 2011 at 15:34 Comment(3)
I added it to my folder containing the views: /layout/razor_views/web.config without any success...Petitioner
It should be in the main web.config or app.configBaldpate
This project has long gone through many changes. Please see the github project github.com/Antaris/RazorEngineBaldpate
C
7

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();
    }
Compensatory answered 24/9, 2015 at 9:1 Comment(0)
B
1

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>
Baldpate answered 16/5, 2011 at 15:34 Comment(3)
I added it to my folder containing the views: /layout/razor_views/web.config without any success...Petitioner
It should be in the main web.config or app.configBaldpate
This project has long gone through many changes. Please see the github project github.com/Antaris/RazorEngineBaldpate

© 2022 - 2024 — McMap. All rights reserved.