Search code examples
asp.net-mvcasp.net-optimization

How do I include a ScriptBundle in a user control?


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?


Solution

  • 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.