有没有一种方法可以确定之前是否呈现过ASP.Net MVC捆绑包? [英] Is there a way to determine an ASP.Net MVC Bundle is rendered before or not?
问题描述
这里是情况:
在MVC应用程序上,我有一个部分,正在呈现脚本捆绑包. 此部分被渲染了几次.
On a MVC application I have a partial that is rendering a script bundle. this partial is rendered several times.
是否有内置的方法来确定特定的捆绑包是在页面的某个位置之前呈现的?
Is there a built-in way to determine an specific Bundle is rendered before in some place on page ?
感谢专业人员
更新:这是清除条件的代码
主布局:
<html>
<body>
@if(someCondition){
@Html.RenderPartial("SamePartial")
@Html.RenderPartial("SamePartial")
@Html.RenderPartial("SamePartial")
}
</body>
</html>
这是局部的:
<div>Blah Blah Blah</div>
@Scripts.Render("~/bundles/JusNeededInsidePartial")
@Styles.Render("~/Themes/styles/JusNeededInsidePartial")
部分渲染多次.我需要一种仅1次渲染包的方法
推荐答案
像在做操作一样,我不太乐意将捆绑软件放入局部视图中.
I'd be a bit leery about putting bundles inside your partial views like you are doing.
For best performance, I'd suggest putting scripts at the bottom of your page (ref: http://developer.yahoo.com/blogs/ydn/high-performance-sites-rule-6-move-scripts-bottom-7200.html)
如果捆绑包中包含样式表,则将其放置在body
中,该样式表仍然有效,但是我希望对其进行组织,以使它们在一起.
If your bundles contain stylesheets, you're going to be putting them in the body
, which is still valid, but I like to organize them so that they're all together.
通常,我要做的就是将它添加到_Layout.cshtml
结束标记之前:
Typically what I would do is add this to my _Layout.cshtml
right before the closing </head>
tag:
@RenderSection("Head", required: false)
然后,在我的View
中,我可以拥有:
Then, in my View
, I could have:
@Html.RenderPartial("SamePartial")
@Html.RenderPartial("SamePartial")
@Html.RenderPartial("SamePartial")
@section head{
@Scripts.Render("~/bundles/myBundle")
}
如果我想在另一个View
上做类似的事情:
And if I wanted to do something similar on another View
:
@Html.RenderPartial("OtherPartial")
@Html.RenderPartial("OtherPartial")
@section head{
@Scripts.Render("~/bundles/myOtherBundle")
}
我知道这与您要的有所不同,但是我认为此解决方案的灵活性很有价值.
I know this is a bit different than what you were asking for, but I think there's value in the flexibility of this solution.
如果要动态加载内容,则始终可以在部分中添加一个简单条件:
And if you're loading things dynamically, you could always put a simple condition in your section:
@section head{
@if(SomeCondition){
@Scripts.Render("~/bundles/modernizr")
}
}
这篇关于有没有一种方法可以确定之前是否呈现过ASP.Net MVC捆绑包?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!