3

我刚开始使用 RazorEngine 并且在使用静态辅助方法时遇到了挫折。它只是为模板生成一个 MvcHtmlString/IHtmlString。当调用 Razor.Parse(...) 我得到

RazorEngine.Templating.TemplateCompilationException : Unable to compile template. 
Der Typ 'System.Web.IHtmlString' ist in einer nicht referenzierten Assembly definiert. (not referenced)
Fügen Sie einen Verweis auf die Assembly 'System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' hinzu.

我添加了对System.Web, System.Web.Mvc,的引用System.Web.WebPages。还尝试将它们添加到cshtml ( @using System.Web, @using System.Web.Mvc) 中。但@using System.Web.Mvc结果在.Mvc不可用System.Web?!

我目前正在尝试在NUnit测试中构建模板。

4

2 回答 2

3

RazorEngine 有一个类 CompilerServicesUtility。这负责在编译视图时加载和使程序集可用。它默认返回当前 AppDomain 中加载的所有程序集。

对于查看代码的人来说,这有点不直观,但确保 Razor 视图编译并且可以使用 MvcHtmlString 的一种方法是使用 AppDomain.Current.Load 将 System.Web 和 System.Web.Mvc 程序集加载到当前 AppDomain ("") 在执行/编译您的视图之前。

于 2014-10-16T15:39:53.063 回答
0

一个老问题。@Gopal 有正确的答案。除了添加以下代码外,我还必须设置对 System.Web 的引用以复制本地。

AppDomain.CurrentDomain.Load("System.Web");
于 2018-10-30T11:51:02.060 回答