asp.netrazorservicestackservicestack-razor

Servicestack include _Layout.cshtml in Razor Content Page


How can I include _Layout.cshtml in Razor Content Page ?

For example I created two cshtml files in root of my project.

First file is _Layout.cshtml

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title></title>
</head>
<body>
    <h1>Main Layout</h1>
    <br>
    <br>   
    @RenderBody();    
</body>
</html>

Second File is Product.cshtml

@inherits ServiceStack.Razor.ViewPage

@{ 
    Layout = "~/_Layout.cshtml";

}

    <h1>Product Page</h1>

When I call http://localhost:6000/product

The result is in browser is

Product Page

but it should be

Main Layout

Product Page

Why ? What's the problem ?


Solution

  • Layout names should be the name of the file not a path and you should never need to reference _Layout as it's the default.

    Also if you want your Views and Content Pages to share the same _Layout.cshtml pages add them once to /Views/_Layout.cshtml or /Views/Shared/_Layout.cshtml instead.

    If this is Self hosting HttpListener project you need to ensure all *.cshtml are set to Copy to Output Directory or the WebHostPhysicalPath references your project path.