简体   繁体   English

从捆绑视图中将Javascript添加到ASP.NET / MVC网页的标题部分?

[英]Add Javascript to header section of an ASP.NET/MVC web page from a view with bundling?

If found this Stack Overflow page that shows you how to add a Javascript include to an ASP.NET/MVC (Razor) view, so that it ends up in the HEAD section of the web page: 如果找到此Stack Overflow页面,它向您展示如何将Javascript include添加到ASP.NET / MVC(Razor)视图中,以便它最终位于Web页面的HEAD部分:

Add CSS or JavaScript files to layout head from views or partial views 从视图或部分视图添加CSS或JavaScript文件到布局头

It works great. 它很棒。 But I'm wondering if there is a way to do it in a way that takes advantage of bundling? 但我想知道是否有办法以利用捆绑的方式来做到这一点? I found this SO post that says it isn't possible and to use something called Cassette, but it's from 2012 so I'm wondering if there's a way to do it now native to ASP.NET/MVC/Razor: 我发现这篇SO帖子说它不可能并且使用名为Cassette的东西,但它是从2012年开始的,所以我想知道现在是否有一种方法可以在ASP.NET / MVC/Razor中使用它:

MVC Bundles: How to add assets in views and have them render combined with the main layout page bundle MVC Bundles:如何在视图中添加资源,并将它们与主布局页面包相结合进行渲染

I found this SO post that talks about adding new bundles dynamically but it appears to add them only to the BODY section, not the HEAD: 我发现这个SO帖子谈到动态添加新的包,但似乎只将它们添加到BODY部分,而不是HEAD:

MVC Bundles: How to add assets in views and have them render combined with the main layout page bundle MVC Bundles:如何在视图中添加资源,并将它们与主布局页面包相结合进行渲染

Your comment has helped me understand what you want to do now. 您的评论帮助我了解您现在想要做什么。 As far as rendering out bundles from a view, it works the same way the answer to your first question. 至于从视图中渲染出束,它的工作方式与第一个问题的答案相同。

First, make a bundle for those file(s) that you want. 首先,为您想要的那些文件制作一个包。

BundleConfig.cs BundleConfig.cs

bundles.Add(new ScriptBundle("~/bundles/mySpecialBundle").Include(
                      "~/Scripts/notOftenUsedScript.js",
                      "~/Scripts/someOtherScript.js"));

Next, in your layout, define a section where you want to be able to render out the JavaScript bundle. 接下来,在您的布局中,定义您希望能够呈现JavaScript包的部分。

_layout.cshtml _layout.cshtml

@RenderSection("scripts", required: false)

Now, in any view that you want to render the special bundle, reference this section and use the same syntax that you would in the layout page for bundle rendering. 现在,在任何要呈现特殊包的视图中,请引用此部分并使用与布局页面中相同的语法进行包呈现。

ExampleView.cshtml ExampleView.cshtml

@section scripts{
    <!---Test section rendering-->
    @Scripts.Render("~/bundles/mySpecialBundle")
}

The @Scripts.Render and @Styles.Render will work from any razor page in your application. @Scripts.Render@Styles.Render可以在您的应用程序中的任何剃刀页面上运行。 Using the section allows us to control where the output from our view page will land in the master page. 使用该部分可以控制我们的视图页面的输出将在主页面中的位置。

With that said, if the JS is truly lightweight, then after the first time your user hits a page with the special bundle, it should be cached by their browser. 话虽如此,如果JS真的是轻量级的,那么在用户第一次点击带有特殊包的页面之后,它应该由他们的浏览器缓存。 So, the performance hit from just having this in your layout all the time would probably be minimal the first page hit and non-existent for each page hit afterwards. 因此,在您的布局中始终保持这种性能的所有时间可能是最小的第一页命中,并且之后每个页面都不存在。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM