JustJohn
JustJohn

Reputation: 1460

How to use _Layout.cshtml in View when application is Razor Pages

In Razor Pages, the _Layout.chstml, is included by default (for all I know there is a setting somewhere). If you don't want the _Layout template on a Razor Page you put:

@{
    Layout = null;
}

However, on an MVC View, you reference a _Layout.chstml:

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

I'm not even sure the same _Layout can be used. I am hoping so and that I just don't have the route syntax for Views inside a Razor Pages application (.NET Core 2.2 Razor Pages).

I tried:

@{
    ViewBag.Title = "ThankYou";
    Layout = "/Shared/_Layout.cshtml";
}

But it triggered a Not Found error.

Upvotes: 0

Views: 1114

Answers (1)

Mike Brind
Mike Brind

Reputation: 30045

Assuming that you want to use the Razor Pages layout in an MVC View, use the virtual file path just like in your MVC example:

@{
    ViewBag.Title = "ThankYou";
    Layout = "~/Pages/Shared/_Layout.cshtml";
}

Incidentally, the "setting" for the layout page in a Razor Pages application is in the _ViewStart.cshtml file. the default version includes the following content:

@{
    Layout = "_Layout";
}

Upvotes: 1

Related Questions