asp.net mvc中的节脚本与脚本标签 [英] section scripts vs script-tag in asp.net mvc

查看:111
本文介绍了asp.net mvc中的节脚本与脚本标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这两个关于脚本"和脚本标签"部分的陈述有何区别?不是脚本中我不感兴趣的内容.

What is the difference of both statements concerning the section Scripts and script-tag? NOT the content inside the scripts that does not interest me.

@section Scripts {
    @Scripts.Render(Bundles.Scripts.Validation)
}

vs

<script src="@Url.Content("~/Scripts/Validation.js")"></script>

推荐答案

第一个会在布局中具有@RenderSection("Scripts")的位置呈现<script>标签.

The first one renders the <script> tag where you have @RenderSection("Scripts") in your layout.

当您不必为所有页面都包含脚本时,首选此 .

This is preferred when you don't have to include a script for all pages.

@Scripts.Render将缩小并捆绑您的脚本.通常,在body 标记的末尾使用标记,以便Views可以在呈现DOM之后获取脚本.

Also @Scripts.Render will minify and bundle your scripts. Usually this is used at the end of body tag so that Views can get the scripts after the DOM is rendered.

第二个仍然保留在您使用<script>标记的位置.

The second one remains where you use the <script> tag.

如果在Layout中使用该脚本,则脚本将包含在所有页面中(例如jQuery).

If you use it in Layout, the script is included in all pages (e.g. jQuery).

让我们举个例子

 <!-- HTML in Layout, before Scrip -->
 @RenderBody()
 <script src="@Url.Content("~/Scripts/jquery.min.js")"></script>
 @RenderSection("Scripts")
 <!-- HTML after Script -->

在这里,如果脚本使用jQuery,则要包含在section中,因为section 之前包含jQuery.

Here, if the script make use of jQuery you want to included with section because jQuery is included before section.

如果在视图中包含<script>,则会给出错误消息,提示jQuery丢失,因为在jQuery 之前包含.

If you include with <script> in your view you will give an error, that jQuery is missing, because is included before jQuery.

这篇关于asp.net mvc中的节脚本与脚本标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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