自定义ViewEngine后未使用_ViewStart

时间:2013-02-18 18:31:19

标签: asp.net asp.net-mvc viewengine

是否有人知道在MVC 3中使用自定义ViewEngine无法获取_ViewStart.cshtml的原因?

我的观点现在位于

〜\ UI \视图\

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

提前致谢, 斯科特

1 个答案:

答案 0 :(得分: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
        );
    }
}