等同于"@section"在ASP.NET Core MVC中? [英] Equivalent of "@section" in ASP.NET Core MVC?

查看:332
本文介绍了等同于"@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="https://ajax.aspnetcdn.com/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="https://ajax.aspnetcdn.com/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中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆