asp.net-mvc 如何在视图中的脚本部分包含脚本包
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13660868/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to include script bundle in scripts section in view
提问by bflemi3
I have a layout with an @RenderSection("scripts")section and I have a bundle that would need to be included in this section for someviews. I thought that just doing this in the view would work, but it's not rendering the scripts.
我有一个包含一个@RenderSection("scripts")部分的布局,我有一个包需要包含在此部分中以获取某些视图。我认为只在视图中执行此操作会起作用,但它不会呈现脚本。
@section scripts {
@Scripts.Render("~/bundles/myBundle")
}
In my view, how can I include a bundle to the scripts section?
在我看来,如何在脚本部分包含一个包?
Layout
布局
@Scripts.Render("~/bundles/jquery", "~/bundles/scripts")
@RenderSection("scripts", required: false)
View
看法
@section scripts {
@Scripts.Render("~/bundles/movie")
}
采纳答案by Yogiraj
Why mix the rendersection with bundling? If you choose to down the route of bundling, you can simply put your scripts in .JS file ,put it in their own bundle if you like and call that bundle on your view. For ex:
为什么将渲染部分与捆绑混合使用?如果您选择走捆绑路线,您可以简单地将您的脚本放在 .JS 文件中,如果您愿意,可以将其放在自己的包中,然后在您的视图中调用该包。例如:
bundles.Add(new ScriptBundle("~/bundles/myscripts").Include(
"~/Scripts/myscript1.js",
"~/Scripts/myscript2.js"));
then view will have the following:
然后视图将具有以下内容:
@Scripts.Render("~/bundles/myscripts")
Also make sure your Web.config has compilation debug set to false like below:
还要确保您的 Web.config 将编译调试设置为 false,如下所示:
<compilation debug="false" />
it makes sure the scripts are bundled and minified.
它确保脚本被捆绑和缩小。
Update
更新
Based on comments and my recent experience, I can see why would we want to use the two together. Perfect case of learning in the community! :) So, if you decide to come back for refactoring, I would make sure there are no typos to begin with. If it still does not work, please let me know what the problem is and I will update the answer accordingly. Thanks everyone!
根据评论和我最近的经验,我可以理解为什么我们要同时使用两者。社区学习的完美案例!:) 所以,如果你决定回来重构,我会确保一开始就没有错别字。如果它仍然不起作用,请让我知道问题是什么,我会相应地更新答案。谢谢大家!
回答by Sampath
Try below mentioned solution inside the View.
在View 中尝试下面提到的解决方案。
View
看法
@section Scripts{
<script src="@System.Web.Optimization.BundleTable.Bundles.ResolveBundleUrl("~/bundles/movie")"></script>
}
回答by Dementic
I can not replicate the issue you are having
我无法复制您遇到的问题
@section scripts {
@Scripts.Render("~/bundles/movie")
}
renders fine with no issue (and respects the debug flag) i would agree with @Mrchref and revisit your paths
呈现没有问题(并尊重调试标志)我会同意@Mrchref 并重新访问您的路径
on the other hand, you could use something like this:
另一方面,你可以使用这样的东西:
public static class Helpers
{
public static HtmlString StaticContent(this System.Web.Mvc.UrlHelper url, string contentPath)
{
return new HtmlString(System.Web.Optimization.Scripts.Render(contentPath).ToString());
}
}
Usage:
用法:
@section Scripts{
@Url.StaticContent("~/assets/js/jquery")
}
i would advise not to use it as is,
You would need to find another solution for System.Web.Optimization.Scripts.Render(contentPath).ToString()since it basically renders the same function you are using (and not working).
我建议不要按原样使用它,您需要找到另一个解决方案,System.Web.Optimization.Scripts.Render(contentPath).ToString()因为它基本上呈现了您正在使用的相同功能(并且不工作)。
You should play around with System.Web.Optimization.BundleTable.Bundlesand see if you can query for both version, check the debug flag, and serve the correct content.
您应该尝试一下,System.Web.Optimization.BundleTable.Bundles看看是否可以查询两个版本,检查调试标志,并提供正确的内容。
回答by Praveen Reddy
I believe a new build would solve your problem. Please follow these steps if not:
我相信新版本可以解决您的问题。如果没有,请按照以下步骤操作:
Step 1:add this into
BundleConfig.csbundles.Add(new ScriptBundle("~/bundles/common").Include("~/Scripts/common.js"));Step 2:add this line in your
view/layout@Scripts.Render("~/bundles/common")Step 3:build your project and run it.
第 1 步:将其添加到
BundleConfig.csbundles.Add(new ScriptBundle("~/bundles/common").Include("~/Scripts/common.js"));第 2 步:在您的
view/layout@Scripts.Render("~/bundles/common")第 3 步:构建您的项目并运行它。

