【问题标题】:_ViewStart not used after Custom ViewEngine_ViewStart 在自定义 ViewEngine 后未使用
【发布时间】:2013-02-18 18:31:18
【问题描述】:

有谁知道 _ViewStart.cshtml 没有被 MVC 3 中的自定义 ViewEngine 选中的原因吗?

我的观点现在在

~\UI\视图\

~\UI\Views\共享\

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

提前致谢, 斯科特

【问题讨论】:

  • 这些视图是否在它们自己的区域内?
  • 有基本视图,然后我设置了一个我注册的示例管理区域。我试着把这个区域拿出来看看它是否能解决它,但似乎没有。 _ViewStart.cshtml 不适用于任何视图。
  • 当然,结果只是一个明显的疏忽……哎呀!我已经为我的问题提供了以下答案。
  • @Scott,我面临同样的问题,以下接受的答案对我没有帮助。有什么想法吗?

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


【解决方案1】:

不幸的是,愚蠢这次赢了。我的自定义 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
        );
    }
}

【讨论】:

    猜你喜欢
    • 2011-08-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多