admin管理员组文章数量:1322838
I'm setting up my MVC4 app to use the built-in ASP.Net bundling (converting from Squishit). I've set up the BundleConfig class, and things all work fine, but for cases where a Razor file only has a reference to one JavaScript file, I don't see the need to involve that config file. I'd rather just add the script to the BundleTable right in my Razor 'Scripts' section, and Render it right there. That way I can do a release to production quite easily for small changes if I have to rather than change the BundleConfig file. This is what I have:
@section Scripts
{
@{
const string bundle = "~/bundles/{myVirtualBundlePath}";
BundleTable.Bundles.Add(new ScriptBundle(bundle)
.Include("{myActualJavaScriptFilePath}"));
Scripts.Render(bundle);
}
}
and while this builds ok, the reference to that bundled file never gets output to HTML.
Do I need to get a reference to the particular BundleCollection that the BundleConfig uses?
I'm setting up my MVC4 app to use the built-in ASP.Net bundling (converting from Squishit). I've set up the BundleConfig class, and things all work fine, but for cases where a Razor file only has a reference to one JavaScript file, I don't see the need to involve that config file. I'd rather just add the script to the BundleTable right in my Razor 'Scripts' section, and Render it right there. That way I can do a release to production quite easily for small changes if I have to rather than change the BundleConfig file. This is what I have:
@section Scripts
{
@{
const string bundle = "~/bundles/{myVirtualBundlePath}";
BundleTable.Bundles.Add(new ScriptBundle(bundle)
.Include("{myActualJavaScriptFilePath}"));
Scripts.Render(bundle);
}
}
and while this builds ok, the reference to that bundled file never gets output to HTML.
Do I need to get a reference to the particular BundleCollection that the BundleConfig uses?
Share Improve this question asked Jan 16, 2013 at 0:34 Ralph LavelleRalph Lavelle 5,7694 gold badges39 silver badges47 bronze badges1 Answer
Reset to default 6Only difference between mine (that works) is that I add the bundle outside of the script block. If this doesn't work make sure it does work in the main bundle start-up, it could be some other issue.
@{ BundleTable.Bundles.Add(new ScriptBundle("~/Scripts/Plugins/highcharts")
.Include("~/Scripts/Plugins/highcharts.js")); }
@section Scripts
{
@Scripts.Render("~/Scripts/Plugins/highcharts")
@Scripts.Render("~/Scripts/Views/Charts/transportDelayCharts")
}
本文标签: javascriptIn MVC4how do I create a script bundle in a Razor fileStack Overflow
版权声明:本文标题:javascript - In MVC4, how do I create a script bundle in a Razor file - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742117401a2421532.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论