ASP.NET MVC 3与Razor和Spark视图引擎并排

时间:2012-06-21 22:47:47

标签: asp.net-mvc razor asp.net-mvc-routing spark-view-engine

我最近从MVC2升级到了MVC3。我们曾经使用Spark视图引擎,我正在尝试迁移到Razor。到目前为止,MVC3的升级成功了。我也升级了Spark视图引擎,因为我需要。

问题是我能够成功渲染Spark和Razor视图但由于某种原因,MVC在一个位置寻找Spark文件而在另一个位置寻找Razor。这就好像Razor没有正确地考虑我的区域但Spark是。

输出:

<pre>
The view 'index' or its master was not found or no view engine supports the searched locations. The following locations were searched:

~/Areas/Live/Views/multimedia/index.aspx
~/Areas/Live/Views/multimedia/index.ascx
~/Areas/Live/Views/Shared/index.aspx
~/Areas/Live/Views/Shared/index.ascx
~/Views/multimedia/index.aspx
~/Views/multimedia/index.ascx
~/Views/Shared/index.aspx
~/Views/Shared/index.ascx
~/Areas/Live/Views/multimedia/index.cshtml
~/Areas/Live/Views/multimedia/index.vbhtml
~/Areas/Live/Views/Shared/index.cshtml
~/Areas/Live/Views/Shared/index.vbhtml
~/Views/multimedia/index.cshtml
~/Views/multimedia/index.vbhtml
~/Views/Shared/index.cshtml
~/Views/Shared/index.vbhtml
Live\~\Areas\Live\Views\multimedia\index.spark
Live\~\Areas\Live\Views\Shared\index.spark
Live\multimedia\index.spark
Live\Shared\index.spark
Live\~\Areas\Live\Views\multimedia\index.shade
Live\~\Areas\Live\Views\Shared\index.shade
Live\multimedia\index.shade
Live\Shared\index.shade
~\Areas\Live\Views\multimedia\index.spark
~\Areas\Live\Views\Shared\index.spark
multimedia\index.spark
Shared\index.spark
~\Areas\Live\Views\multimedia\index.shade
~\Areas\Live\Views\Shared\index.shade
multimedia\index.shade
Shared\index.shade
</pre>

如果我将.cshtml文件移动到MVC希望它的位置,它将起作用,但这不会削减它。为什么两个引擎会在略有不同的位置寻找?

1 个答案:

答案 0 :(得分:0)

从我看过的内容看来,Razor和Spark似乎以不同的方式寻找视图(无论如何使用区域)。我不完全确定这一般是否属实,或者如果我正在研究这个实现的某些东西是不同的。我设法通过扩展Razor引擎来遵循我们项目所采用的相同模式来解决我的问题。

  public class CustomRazorViewEngine : RazorViewEngine
  {
      public CustomRazorViewEngine(): base()
      {
          AreaMasterLocationFormats = new string[] { "~/Areas/{2}/Views/{1}/%1/{0}.cshtml",
                             "~/Areas/{2}/Views/{1}/%1/{0}.vbhtml",
                             "~/Areas/{2}/Views/Shared/%1/{0}.cshtml",
                             "~/Areas/{2}/Views/Shared/%1/{0}.vbhtml" };

          AreaPartialViewLocationFormats = new string[] { "~/Views/{2}/Shared/{0}.cshtml" };

          AreaViewLocationFormats = new string[] { "~/Views/{2}/{1}/{0}.cshtml",
                       "~/Views/{2}/Shared/{0}.cshtml" };

          ViewLocationFormats = new string[] { "~/Views/{1}/{0}.cshtml",
                       "~/Views/Shared/{0}.cshtml" };

          MasterLocationFormats = new string[] { "~/Views/{1}/%1/{0}.cshtml",
                         "~/Views/{1}/%1/{0}.vbhtml",
                         "~/Views/Shared/%1/{0}.cshtml",
                         "~/Views/Shared/%1/{0}.vbhtml" };

          PartialViewLocationFormats = new string[] { "~/Views/{1}/{0}.cshtml",
                          "~/Views/{1}/{0}.vbhtml",
                          "~/Views/Shared/{0}.cshtml",
                          "~/Views/Shared/{0}.vbhtml" };

          FileExtensions = new string[] { "cshtml", "vbhtml" };

      }

  }