MVC 6 VNext如何从不同区域设置相同的布局

Car*_*ire 4 c# asp.net-mvc razor

如何在MVC 6 Vnext中从不同区域设置相同的布局,

我在每个区域使用_ViewImports.cshmlt和_ViewStart.cshtml

在_ViewStart.cshtml中

@{
    Layout = "_/Views/Shared/_Layout.cshtml";
}
Run Code Online (Sandbox Code Playgroud)

和_ViewImports.cshtml

@using Cross2Enterprise.Administrador
@using Cross2Enterprise.Administrador.Models
@using Microsoft.Framework.OptionsModel
@addTagHelper "*, Microsoft.AspNet.Mvc.TagHelpers"
Run Code Online (Sandbox Code Playgroud)

Rob*_*sen 6

我认为这是你的情景:

  1. ASP.NET Core w/MVC
  2. 使用\Areas文件夹
  3. 您想要在一个位置设置所有区域使用的模板

为此,请使用以下项目布局:

\Areas\Home
\Areas\Home\Controllers
\Areas\Home\Views
\Areas\_ViewStart.cshtml
\Views
\Views\Shared\
\Views\Shared\_Layout1.cshtml
\Views\Shared\_Layout2.cshtml
Run Code Online (Sandbox Code Playgroud)

然后在文件中,\Areas\_ViewStart.cshtml您可以拥有以下内容:

@{
    Layout = "_Layout1";
}
Run Code Online (Sandbox Code Playgroud)

这将使所有区域的所有视图都使用\Views\Shared\_Layout1.cshtml.

注意:您不能放入_ViewStart.cshtml以下位置:

\Views\_ViewStart.cshtml
\Views\Shared\_ViewStart.cshtml
Run Code Online (Sandbox Code Playgroud)

注意:您CAN_ViewStart.cshtml在以下位置:

\Areas\Home\_ViewStart.cshtml
Run Code Online (Sandbox Code Playgroud)

注意:这也适用于_ViewImports.cshtml


Jes*_*ulo 5

我只是贬低了明显的事情......你试过......?(我只看了一些关于VNext的事情)

Layout = "~/Views/Shared/_Layout.cshtml"; 
Run Code Online (Sandbox Code Playgroud)