自托管 Nancy 控制台应用程序中的 Razor 视图引擎问题

发布于 2024-12-13 13:48:36 字数 1019 浏览 2 评论 0原文

有些我正在运行 Nancy Web Framework 自托管演示的稍微修改版本,Nancy.Demo.Hosting.Self。我对其进行了修改,以包含 Nancy 的 Razor 视图引擎 Nancy.ViewEngines.Razor。当我使用基本的 Razor 功能时,它工作得很好,但我在使用 @Render 部分视图和布局时遇到了麻烦。

这些高级功能在 ASP.NET 之外是否受支持?

我从 Nancy.Demo.Hosting.Aspnet 复制的相同视图似乎在那里工作得很好。

我因找不到“标题”而崩溃。

这是视图:

@{ 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>
}

和布局

<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>

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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

等数载,海棠开 2024-12-20 13:48:36

您确定您的头 cshtml 文件设置为复制到输出目录吗?

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

ˉ厌 2024-12-20 13:48:36

工作正常,这是我的_Layout.cshtml(注意@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>© 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>

这是我的内容,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>

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>© 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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文