Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ViewStart.cshtml does not seem to be read from views

I am building a new ASP.Net Core 2.0 from the empty site template and I am trying to get the _ViewStart.cshtml page working. I have crated the Views -> Shared -> _ViewStart.cshtml page with the following code in it:

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

In my Index.cshtl file I use for my landing page view I have this code:

@{
    ViewData["Title"] = "Index";
    //Layout = "~/Views/Shared/Layout/_Layout.cshtml";
}

<h2>Index</h2>

If I run an F5 debug in Visual Studio 2017 the index page loads up and just shows "Index", if I look at the source code there is no HTML from my layout. If I uncomment the Layout line and reload the page everything works fine. The two layout paths are identical in both files so why is the Index.cshtml file not reading from _ViewStart.cshtml?

I am not sure if I just missed adding a package or something to make this work or does ViewStart not work in .NET Core liked it did in .NET 4.5?

like image 683
Matthew Verstraete Avatar asked Nov 05 '25 04:11

Matthew Verstraete


1 Answers

It works like it did before. This is a case of putting it in the wrong folder.

It (_ViewStart.cshtml) is suppose to be in the root view folder Views/_ViewStart.cshtml not the Views/Shared folder

Neither _ViewStart.cshtml nor _ViewImports.cshtml are typically placed in the /Views/Shared folder. The app-level versions of these files should be placed directly in the /Views folder.

Reference Layout in ASP.NET Core : Running Code Before Each View

like image 150
Nkosi Avatar answered Nov 09 '25 10:11

Nkosi