19

Is there a way to replicate the behaviour of the @:Scripts/Styles.Render helper from code behind?

If I reference the bundles by using the BundleTable.Bundles.ResolveBundleUrl method, then bundling and minification occurs even with debug=true. I've read other questions, and the solution seems far obvious, by using the previously mentioned helper. But in my case, I don't know the name of the bundle while in the aspx, and it's generated in runtime in the code behind.

So, I need to add the references in the head, from the code behind, and I can't find a way to do it.

Is there a way? Or will I be forced to do it in the .aspx file?

Alejandro Rizzo
  • 837
  • 2
  • 8
  • 14
  • Why can't you just call these helpers in your codebehind to get the script/style references that you want to render? – Hao Kung Jan 23 '13 at 19:41

3 Answers3

26

Yep!

This is what I do for Web Forms. This example adds resources to the <head> but also works with any control for which Controls.Add() works

For CSS:

System.Web.UI.WebControls.Literal lit = new System.Web.UI.WebControls.Literal();
lit.Text = System.Web.Optimization.Styles.Render("~/bundles/my_css").ToHtmlString();
Header.Controls.Add(lit);

For JS:

System.Web.UI.WebControls.Literal lit = new System.Web.UI.WebControls.Literal();
lit.Text = System.Web.Optimization.Scripts.Render("~/bundles/my_js").ToHtmlString();
Header.Controls.Add(lit);

Also - since ASPX is a subclass of codebehind, you theoretically COULD get to the bundle name from ASPX by making it a protected (or public) variable in the codebehind (but you didn't post all your code so I'm not sure what you're up to exactly).

nothingisnecessary
  • 6,099
  • 36
  • 60
6

You could also just render the script inline

<%: Scripts.Render("~/bundles/my_js") %>
Ishmaeel
  • 14,138
  • 9
  • 71
  • 83
Daniel Bardi
  • 77
  • 1
  • 1
4

This also may be of help for those who have come here later.

An alternate option without using a Literal Control:

  ClientScript.RegisterStartupScript(typeof(Page), "MyJS", Scripts.Render("~/bundles/bundledJS").ToHtmlString(), false);
Mozahler
  • 4,958
  • 6
  • 36
  • 56
Arjun Shetty
  • 1,575
  • 1
  • 15
  • 36
  • 1
    Another alternative. `ClientScript.RegisterClientScriptInclude(typeof(Page), "MyJS", Scripts.Url("~/bundles/bundledJS").ToHtmlString());` – Stuart Nov 05 '14 at 12:31
  • 1
    This may be a poor option as it doesn't render the tag in the header, so if there's dependencies above, it will cause javascript errors. – monkeybuffer Jan 14 '16 at 21:53
  • 1
    This option works ok but yeah, it doesn't give you control over where things are rendered. Though I believe you can use `RegisterStartupScript` , etc., for different behavior. Ultimately what I did was create my own server control to be able to do this declaratively. It's super sweet, see [my solution here](http://stackoverflow.com/questions/17437800/how-do-i-use-asp-net-bundling-and-minification-without-recompiling/17689212#17689212) – nothingisnecessary Apr 26 '16 at 21:52