1

我最近从 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 想要的位置,它将起作用,但这不会削减它。为什么这两个引擎的位置会略有不同?

4

1 回答 1

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" };

      }

  }
于 2012-07-11T17:28:35.150 回答