【问题标题】:Area shared layout _ViewStart.cthml区域共享布局_ViewStart.cshtml
【发布时间】:2019-11-02 20:55:14
【问题描述】:

我有多个区域,我想将我的布局指向我的共享区域文件夹。 我的文件夹是

App
 -...
 -Model
   -...
 -Controller
   -...
 -View
    |-Shared
       - _layout.cshtml <--- root layout
       - _layout2.cshtml <--- workaround 
    - _ViewStart.cshtml <--- OK!
 -Areas
  |-Areaname1
    |-Controllers
    |-Views
      |-Shared
        - _layout.cshtml <--- my area layout
      - _ViewStart.cshtml <--- i want to use the layout above
  |-Areaname2
    -...

我的AreasViews/_ViewStart.chtml里面的代码

@{
// Layout = "~/Views/Shared/_Layout.cshtml"; <--- will point on the root layout
// Layout = "~/Views/Shared/_Layout2.cshtml"; <--- view to root
// Layout = "~Area/Areaname1/Views/Shared/_Layout.cshtml"; <--- correct path
// Layout = "~/Areaname1/Views/Shared/_Layout.cshtml"; <--- same error above
}

将布局指向一个区域。

【问题讨论】:

  • 你确定你有标准的视图引擎吗?默认情况下,它应该更喜欢您当前区域的视图。
  • @ChristianGollhardt 感谢您的回复。是的,它是一个全新的 mvc 5 应用程序。
  • 你能解释一下(用文字)你的解决方法正在解决什么问题(与没有解决方法相比)吗?我不确定,你在这里问什么。
  • 我实际上并没有在我的解决方法中做任何事情。问题是位于实际文件夹的路由。默认情况下,布局指向全局根视图文件夹,而不是该区域,无论您的_viewstart.cshtml在哪里。

标签: c# asp.net model-view-controller


【解决方案1】:

通过使用

@{
     Layout = "~/Views/Shared/_Layout.cshtml"; <--- will point on the root layout
}

您明确地说,查看根目录 (~) 并使用 /Views/Shared/_Layout.cshtml

你可能想使用:

@{
     Layout = "_layout.cshtml";
}

这首先会在您的区域文件夹中查找,如果在那里没有找到任何_layout.cshtml,它将在~/Views/Shared/ 文件夹中查找。

您可以在RazorViewEngine 中设置查找路径或向其注册一些习惯。以See the source code 为例。

默认情况下:

  • "~/Areas/{2}/Views/{1}/{0}.cshtml",
  • "~/Areas/{2}/Views/{1}/{0}.vbhtml",
  • "~/Areas/{2}/Views/Shared/{0}.cshtml",
  • “~/Areas/{2}/Views/Shared/{0}.vbhtml”
  • "~/Views/{1}/{0}.cshtml",
  • "~/Views/{1}/{0}.vbhtml",
  • "~/Views/Shared/{0}.cshtml",
  • “~/Views/Shared/{0}.vbhtml”

地点:

  • 2 = 面积
  • 1 = 控制器
  • 0 = 操作/视图(例如_layout

【讨论】:

  • Layout = "_layout.cshtml"; 会给我一个System.Web.HttpException: 'The layout page "_Layout.cshtml" could not be found at the following path: "~/Areas/AreaName1/Views/_Layout.cshtml".'
  • 会不会是,_Layout.cshtml 偶然包含另一个 _Layout.cshtml?在这种情况下,请在子布局中使用完整路径,例如~/Views/Shared/_Layout.cshtml@JuliusLimson
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-11-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多