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

标签 c# asp.net-mvc razor

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

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

在 _ViewStart.cshtml 中

@{
    Layout = "_/Views/Shared/_Layout.cshtml";
}

和_ViewImports.cshtml

@using Cross2Enterprise.Administrador
@using Cross2Enterprise.Administrador.Models
@using Microsoft.Framework.OptionsModel
@addTagHelper "*, Microsoft.AspNet.Mvc.TagHelpers"

最佳答案

我认为这是你的场景:

  1. 带有 MVC 的 ASP.NET Core
  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

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

@{
    Layout = "_Layout1";
}

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

注意:您可以_ViewStart.cshtml 放在以下位置:

\Views\_ViewStart.cshtml
\Views\Shared\_ViewStart.cshtml

注意:您可以_ViewStart.cshtml 放在以下位置:

\Areas\Home\_ViewStart.cshtml

注意:这也适用于 _ViewImports.cshtml

关于c# - MVC 6 VNext 如何从不同的区域设置相同的布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31479451/

相关文章:

c# - asp.net 值 ' 1 ' 无效

asp.net-mvc - 如何为 Razor View 引擎获取 IntelliSense

ajax - 从一个主页保存多个局部 View

c# - 使用 DbContext 的服务器端计时器

c# - 在 C#.NET 中使用特定模式修剪字符串值

javascript - 在 JS 文件中访问 ViewModel (asp.net MVC)

javascript - 下拉列表宽度适合所选元素文本

asp.net-mvc - MVC 中下拉列表的验证

c# - ViewStart 和 Layout - 有什么区别?

c# - 将字符串中单词的第一个字符大写