【问题标题】:ASP.NET MVC 3 with Razor and Spark view engine side by sideASP.NET MVC 3 与 Razor 和 Spark 视图引擎并排
【发布时间】:2012-06-24 06:21:01
【问题描述】:

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

【问题讨论】:

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


    【解决方案1】:

    根据我的阅读,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" };
    
          }
    
      }
    

    【讨论】:

      猜你喜欢
      • 2011-03-11
      • 1970-01-01
      • 2011-07-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多