区域共享布局_ViewStart.cthml

时间:2019-06-20 04:50:20

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

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

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
    -...

我的Areas Views/ _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
}

将布局指向一个区域。

1 个答案:

答案 0 :(得分:2)

通过

@{
     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”,
  • “〜/区域/ {2} /视图/共享/{0}.cshtml”,
  • “〜/区域/ {2} /视图/共享/{0}.vbhtml”
  • “〜/ Views / {1} / {0} .cshtml”,
  • “〜/ Views / {1} / {0} .vbhtml”,
  • “〜/ Views / Shared / {0} .cshtml”,
  • “〜/ Views / Shared / {0} .vbhtml”

位置:

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