使用Razor Pages @RenderSection在Razor Page上似乎不可用,我得到"RenderSection在当前上下文中不存在“
我有一些Razor页面,我试图使用部分页面上的部分页面是一些附带的css和Javascript,我想注入到css和脚本部分
如果我只是在部分页面上定义部分,则不会呈现任何内容,因此经过一些搜索后,似乎在使用部分的页面上需要在各个部分中添加RenderSection。
任何帮助都将不胜感激。
发布于 2019-01-27 11:24:46
您可能错过了在ASP.NET核心中使用@RenderSection的要点。@RenderSection只能在Layout页面中出现,如下所示:
<script src="~/lib/jquery/dist/jquery.js"></script>
@RenderSection("Scripts", required: false)则Razor页面应如下所示:
@{
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的过程中,您的剃刀页面上的scripts将呈现如下:
<script src="~/lib/jquery/dist/jquery.js"></script>
<script src="~/js/yourjs.js" asp-append-version="true"></script>希望它能让你明白!
https://stackoverflow.com/questions/54384703
复制相似问题