我刚刚开始使用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测试中构建模板。
发布于 2014-10-16 15:39:53
RazorEngine有一个类CompilerServicesUtility。这负责在编译视图时加载和使程序集可用。默认情况下,它返回当前AppDomain中加载的所有程序集。
对于查看代码的人来说,这有点不直观,但是确保Razor视图编译并且可以使用MvcHtmlString的一种方法是在执行/编译视图之前使用AppDomain.Current.Load("")将System.Web和System.Web.Mvc程序集加载到当前的AppDomain中。
发布于 2018-10-30 11:51:02
一个老问题。@Gopal有正确的答案。除了添加以下代码外,我还必须设置对System.Web的引用,以复制本地代码。
AppDomain.CurrentDomain.Load("System.Web");https://stackoverflow.com/questions/17105370
复制相似问题