剃刀页面上的渲染部分

时间:2019-01-27 02:59:08

标签: asp.net-core razor-pages

使用Razor页面@RenderSection在Razor页面上似乎不可用,我得到“ RenderSection在当前上下文中不存在”

我有很多Razor页面,我试图在部分页面上使用部分页面,这是一些我想注入到css和脚本部分的随附的CSS和Javascript

如果我仅在部分页面上定义部分,则不会呈现任何内容,因此在进行一些搜索后,似乎在使用部分页面的页面上需要在各个部分中添加RenderSection。

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

可能是您错过了在ASP.NET Core中使用@RenderSection的要点。 @RenderSection应该仅在Layout页中,如下所示:

 <script src="~/lib/jquery/dist/jquery.js"></script>
 @RenderSection("Scripts", required: false)

然后“剃刀”页面应如下所示:

@{
    ViewData["Title"] = "My Razor Page";

    Layout = "_Layout"; // If this specified in the `_ViewStart.cshtml` then you don't need it
}

<partial name="_YourPartial.cshtml"/>
@section scripts {
    <script src="~/js/yourjs.js" asp-append-version="true"></script> // this is specific to this Razor page only and it will also be available on the partial view called inside this Razor Page
}

在生成html的过程中,Razor页面上的scripts将呈现如下:

<script src="~/lib/jquery/dist/jquery.js"></script>
<script src="~/js/yourjs.js" asp-append-version="true"></script>

希望它会让您清楚!

相关问题