I have some legacy ASP.NET WebForms code that uses RegisterClientScriptInclude in a class that inherits from UserControl. I want to replace the multiple calls to RegisterClientScriptInclude with a single new-fangled Bundle. How do I resolve a bundle URL from my bundle route, deep inside a compiled assembly?
Asked
Active
Viewed 722 times
2 Answers
1
Assuming you've registered the bundle already and just want to generate a reference to the bundle.
The simplist way is to just call
BundleCollection.ResolveBundleUrl(<path to bundle, i.e. "~/bundles/mybundle">).
Note by not using the Scripts helper to render out the tags, you will always get a reference to the bundle, there won't be the automatic non bundle functionality when you call the ResolveBundleUrl directly.

Hao Kung
- 28,040
- 6
- 84
- 93
-
"Automatic non-bundle functionality"? – Scott Stafford Jan 03 '13 at 00:36
-
Meaning bundling and minification is off in debug mode when using the Scripts/Styles helpers – Hao Kung Jan 07 '13 at 23:00
0
Try this method:
private void RegisterJavaScriptBundle(string bundleIdent)
{
var bundleUrl = String.Format("~/bundles/{0}", bundleIdent);
var name = String.Format("Bundle{0}", bundleIdent);
if (ScriptManager.ScriptResourceMapping.GetDefinition(name) == null)
ScriptManager.ScriptResourceMapping.AddDefinition(name, new ScriptResourceDefinition { Path = bundleUrl });
ScriptManager.RegisterNamedClientScriptResource(this.Page, name);
}
Than call it in the load event of the usercontrol:
this.RegisterJavaScriptBundle("MyBundle");
This will generate reference to the script with the content hash:
<script src="/bundles/MyBundle?v=B6mdgLTYJ2gPQv_XxE07ACAAaNA6Lo4L_KxViWZE-CY1" type="text/javascript"></script>

Keeble
- 66
- 1
- 6