Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC 6 VNext How to set same layout from different areas

How to set same layout from different areas in MVC 6 Vnext,

I am using _ViewImports.cshmlt and _ViewStart.cshtml in each area

in _ViewStart.cshtml

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

and _ViewImports.cshtml

@using Cross2Enterprise.Administrador
@using Cross2Enterprise.Administrador.Models
@using Microsoft.Framework.OptionsModel
@addTagHelper "*, Microsoft.AspNet.Mvc.TagHelpers"
like image 685
Carlos Freire Avatar asked Dec 14 '22 13:12

Carlos Freire


2 Answers

I think this is your scenario:

  1. ASP.NET Core w/ MVC
  2. Using an \Areas folder
  3. You want to set the template used by all areas in one location

To do this have the following project layout:

\Areas\Home
\Areas\Home\Controllers
\Areas\Home\Views
\Areas\_ViewStart.cshtml
\Views
\Views\Shared\
\Views\Shared\_Layout1.cshtml
\Views\Shared\_Layout2.cshtml

Then in the file \Areas\_ViewStart.cshtml you can have the following:

@{
    Layout = "_Layout1";
}

This will make all views in all areas use \Views\Shared\_Layout1.cshtml.

NOTE: You can NOT put _ViewStart.cshtml in the following locations:

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

NOTE: You CAN put _ViewStart.cshtml in the following location:

\Areas\Home\_ViewStart.cshtml

NOTE: This also applies to _ViewImports.cshtml

like image 164
Robert Paulsen Avatar answered Dec 24 '22 03:12

Robert Paulsen


I'm only discarting the obvious things... Did you tried with ...? (I only have viewed a few of things about VNext )

Layout = "~/Views/Shared/_Layout.cshtml"; 
like image 45
Jesus Angulo Avatar answered Dec 24 '22 04:12

Jesus Angulo