4

有谁知道 _ViewStart.cshtml 是否有理由在 MVC 3 中使用自定义 ViewEngine 不被选中?

我的观点现在住在

〜\用户界面\视图\

~\UI\视图\共享\

ViewStart 位于 ~\UI\Views_ViewStart.cshtml。

我已经清除了现有的 RazorViewEngine 并在 global.asax 中将其替换为我的,并且所有视图都正确解析,除非我在每个视图中单独指定它,否则不会应用任何布局页面。

我的引擎路径格式代码是:

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

        this.PartialViewLocationFormats = new[]
                                              {
                                                  "~/UI/Views/Shared/{0}.cshtml", 
                                                  "~/UI/Views/Shared/Partial/{0}.cshtml", 
                                                  "~/UI/Views/{1}/Partial/{0}.cshtml"
                                              };

        this.AreaMasterLocationFormats = new[] 
                                            { 
                                                "~/UI/Views/Admin/Shared/{0}.cshtml" 
                                            };

        this.AreaPartialViewLocationFormats = new[]
                                                  {
                                                      "~/UI/Views/Admin/Shared/{0}.cshtml", 
                                                      "~/UI/Views/Admin/Shared/Partial/{0}.cshtml"
                                                  };

        this.AreaViewLocationFormats = new[] { "~/UI/Views/Admin/{1}/{0}.cshtml" };

    this.MasterLocationFormats = new[]
    {
         "~/UI/Views/{1}/{0}.cshtml",
         "~/UI/Views/Shared/{0}.cshtml"
    };

在此先感谢,斯科特

4

1 回答 1

2

不幸的是,愚蠢这次赢了。我的自定义 ViewEngine 是基于我从一篇文章中引用的一些代码。在文章中,他们详细介绍了 CreateView 的覆盖。它有一个布尔参数 (runViewStartPages) 设置为 false,但由于它不是命名参数,所以我错过了它。

public class XyzViewEngine : RazorViewEngine
{    
    protected override IView CreateView(ControllerContext controllerContext, string viewPath, string masterPath)
    {
        return new RazorView(
            controllerContext,
            viewPath,
            masterPath,
            true, //<--- this drives whether to use _ViewStart pages.  It was set to false
            FileExtensions,
            ViewPageActivator
        );
    }
}
于 2013-02-19T22:48:49.193 回答