We are currently using the Razor View engine to render out HTML (outside of an ASP.NET MVC project). Everything has been working okay until today when I added an enum type to my model that resides in another assembly. Now when I compile, I get the following error:
"RazorEngine.Templating.TemplateCompilationException was unhandled
HResult=-2146233088 Message=Unable to compile template. The type 'ClassLibrary1.MyClass.MyEnum' is defined in an assembly that is not referenced. You must add a reference to assembly 'ClassLibrary1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'."
I was able to reproduce this error in a simply console application that references a type from an external DLL:
Template:
@model RazorExample.MyModel
<div>
@if(Model.EnumValue == ExternalLib.MyEnum.Val1) { <p>My value is 1</p> }
</div>
Enum (defined in ExternalLib):
namespace ExternalLib
{
public enum MyEnum
{
Val1 = 1,
Val2 = 2
}
}
Model:
using ExternalLib;
namespace RazorExample
{
public class MyModel
{
public String Name { get; set; }
public MyEnum EnumValue{ get; set; }
}
}
Code to compile the template:
Razor.Compile(template.ToString(), "MyTemplate");
I have also tried adding a "@using ExtneralLib;" to the template but that results in an error that the type "ExternalLib" could not be found. I have seen one or two other posts regarding something similar (Razor-based view doesn't see referenced assemblies) but this is not in the context of a web application. Any help is appreciated.
EDIT
I spoke a little too soon that this fixed my error yesterday. It fixed my error in my test app but when I went to update my code, it did not work. After looking at it a bit more, I realized my model was a little more complicated.
I have updated my sample app accordingly:
Model:
public class MyModel
{
public String Name { get; set; }
public ParentClass ParentClass { get; set; }
}
ParentClass:
namespace ExternalLib
{
public class ParentClass
{
public string Name { get; set; }
public ChildClass ChildClass { get; set; }
}
}
ChildClass:
namespace ExternalLib
{
public class ChildClass
{
public enum MyEnum
{
Val1 = 1,
Val2 = 2
}
}
}
Model:
@model RazorExample.MyModel
<div>
@if(Model.ParentClass.ChildClass.EnumValue == ExternalLib.ChildClass.MyEnum.Val1) { <p>My value is 1</p> }
</div>
With this code, if I add the @using ExternalLib; statement into the model, I get the error "Unable to compile template. The type or namespace name 'ExternalLib' could not be found (are you missing a using directive or an assembly reference?)"
If I leave the @using statement out, I get the exception "Unable to compile template. The type 'ExternalLib.ParentClass' is defined in an assembly that is not referenced. You must add a reference to assembly 'ExternalLib, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'."
FIX (or workaround at least)
I was able to fix this in the code I am trying to get working by creating a separate model and template for the child class and using an @Include statement in the template to import it in. I can post that code if anyone is interested but I fear I am already a little long on the post.