试图使用Nustache共享客户端和服务器的Moustache模板,但是Nustache在我的应用程序中表现不佳。我在源代码中直接使用了他们的MVC应用程序示例中的代码,但是每次尝试设置或添加 View 引擎时都会遇到错误。这是一个代码片段(从一种操作方法中,我也尝试过在global.asax中全局添加 View 引擎,并且具有相同的错误):

ViewResult viewResult = View(new { test = "Jawesome" });

viewResult.ViewEngineCollection = new ViewEngineCollection
                                  {
                                      new NustacheViewEngine()
                                  };

这是错误:
[ArrayTypeMismatchException: Attempted to access an element as a type incompatible with the array.]
   System.Collections.Generic.List`1.Insert(Int32 index, T item) +62
   MyController.Index() in C:\src\projects\myproject\myproject.Web\Controllers\MyController.cs:83
   lambda_method(Closure , ControllerBase , Object[] ) +79
   System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters) +264
   ...

最佳答案

我在Nustache上使用MVC4,却遇到了完全相同的问题。我花了一段时间才找出问题所在,但我在网上找不到任何解决方案,因此我想将自己的解决方案发布出来,希望能对其他人有所帮助。

问题是Nustache.Mvc3项目引用了MVC3中的System.Web和System.Web.Mvc,因此我不得不对其进行更新以使用MVC4。要做到这一点:
1.在Visual Studio中,右键单击Nustache.Mvc3项目,然后选择“属性”。
2.在“应用程序”选项卡中,将“目标框架”更改为.Net Framework 4.5
3.返回解决方案资源管理器,删除Nustache.Mv3中“引用”下的System.Web和System.Web.Mvc。
4.右键单击“引用”,然后为System.Web和System.Web.Mvc添加版本4.0。

10-05 20:40