Matthew Verstraete
Matthew Verstraete

Reputation: 6781

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?

Upvotes: 4

Views: 3241

Answers (1)

Nkosi
Nkosi

Reputation: 247018

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

Upvotes: 7

Related Questions