kenny
kenny

Reputation: 22334

Problems with Razor view engine in Self-hosted Nancy Console App

Some I'm running a slightly modified version of the Nancy Web Framework self hosting demo, Nancy.Demo.Hosting.Self. I've modified it to include the Nancy's Razor view engine, Nancy.ViewEngines.Razor. It works fine when I use basic Razor features, but I've run into trouble with @Render partial views and layouts.

Are these advanced features supported outside of ASP.NET?

The same views I copied from Nancy.Demo.Hosting.Aspnet, seem to work fine there.

I'm getting a crash about not finding my 'Header'.

Here's the view:

@{ Layout = "razor-layout.cshtml"; }
@section Header {
    <!-- This comment should appear in the header -->
}
<h1>Hello @Model.FirstName</h1>
<p>This is a sample Razor view!</p>
@section Footer {
    <p>This is footer content!</p>
}

And the Layout

<html>
<head>
    <title>Razor View Engine Demo - @Model.FirstName</title>
    @RenderSection("Header")
</head>
<body>
    <div id="body">@RenderBody()</div>
    <div id="footer">@RenderSection("Footer")</div>
    <div id="optional">@RenderSection("Optional", false)</div>
</body>
</html>

Upvotes: 4

Views: 1759

Answers (2)

John P
John P

Reputation: 1

Works fine, here is my _Layout.cshtml (note the @RenderBody):

@inherits Nancy.ViewEngines.Razor.NancyRazorViewBase
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Test Owin</title>
    <link href="/Content/bootstrap/bootstrap.css" rel="stylesheet" />
</head>
<body>
   <div class="container body-content">
        @RenderBody()
        <hr />
        <footer>
            <p>&copy; 2016 - My Test Owin Application</p>
        </footer>
    </div>
    <script src="/Scripts/jquery-3.1.1.js"></script>
    <script src="/Scripts/bootstrap.js"></script>
</body>
</html>

and here is my content, Index.cshtml:

@{ Layout = "_Layout.cshtml"; }

<div class="jumbotron">
    <h1>Test Owin</h1>
    <p class="lead">Test</p>
    <p>
        Yesterday, Elon Musk got on stage at the 2016 International Astronautical Congress and unveiled the first real details about the big fucking rocket they’re making.
    </p>
</div>

Upvotes: 0

Steven Robbins
Steven Robbins

Reputation: 26599

Are you sure your header cshtml file is set to copy to the output directory?

Upvotes: 4

Related Questions