等同于"@section"在ASP.NET Core MVC中?

编程入门 行业动态 更新时间:2024-10-27 00:27:32
本文介绍了等同于"@section"在ASP.NET Core MVC中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在默认的_Layout.cshtml文件中,脚本在环境"中定义,如下所示:

In the default _Layout.cshtml file, scripts are defined in "environment"s like so:

<environment names="Development"> <script src="~/lib/jquery/dist/jquery.js"></script> <script src="~/lib/bootstrap/dist/js/bootstrap.js"></script> <script src="~/js/site.js" asp-append-version="true"></script> </environment> <environment names="Staging,Production"> <script src="ajax.aspnetcdn/ajax/jquery/jquery-2.1.4.min.js" asp-fallback-src="~/lib/jquery/dist/jquery.min.js" asp-fallback-test="window.jQuery"> </script> <script src="ajax.aspnetcdn/ajax/bootstrap/3.3.5/bootstrap.min.js" asp-fallback-src="~/lib/bootstrap/dist/js/bootstrap.min.js" asp-fallback-test="window.jQuery && window.jQuery.fn && window.jQuery.fn.modal"> </script> <script src="~/js/site.min.js" asp-append-version="true"></script> </environment>

下面是@RenderSection("scripts", required: false)

我似乎无法在任何单独的.cshtml文件中实现一个部分(在本例中为脚本"),因为看起来它们已在Core中摆脱了"@section"

I can't seem to implement a section (in this case "scripts") in any separate .cshtml file since it looks like they got rid of "@section" in Core

我想为特定视图添加特定脚本.解决这个问题的新方法是什么?我现在是否将所有内容都转储到_Layout中?

I would like to add specific scripts for specific views. What is the new way to go about this? Do I just dump everything in _Layout now?

推荐答案

我认为您错了.它在ASP.NET Core中工作正常.我的_layout.cshtml

I think you are mistaken. It works just fine in ASP.NET Core. I have this in my _layout.cshtml

@RenderSection("scripts", required: false)

在我的一种观点中,我正在像这样添加脚本:

and in one of my views I'm adding scripts there like this:

@section Scripts { @if (Model.CanEdit) { await Html.RenderPartialAsync("EditorScriptsPartial"); } }

更多推荐

等同于"@section"在ASP.NET Core MVC中?

本文发布于:2023-11-15 10:38:24,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1593683.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:ASP   section   quot   MVC   Core

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!