Updated MVC Script Helpers

Posted by Matthew Osborn on December 18, 2009

Okay so if you read this title and said “wait… MVC has Script helpers?!” you should first go read my original post on MVC Script & Css Helpers. Okay now for those of you who know what they are or just finished reading about them, I was able to add a small feature from Beta to RC to the script helper that I just didn’t have time to code the first time. If you download the new RC futures assembly and add it to your project or update the existing one you will notice that the script helper now has an overload.

If you have not been able to figure out what this overload does, it allows you to specify a path to a second file to use when you are debugging. For instance it is pretty common to minify JavaScript which basically makes it impossible to read or understand. Most developer maintain to versions on the JavaScript one minified and one not for debugging purposes. In fact if you are using the ScriptManager control in ASP.NET this has the same basic functionality.

  1. <head>
  2.     <title><asp:ContentPlaceHolder ID="TitleContent" runat="server" /></title>
  3.     <%= Html.Css("Site.css") %>
  4.     <%= Html.Script("MicrosoftAjax.js", "MicrosoftAjax.debug.js") %>
  5. </head>

So you might be asking how we know when to render the debug script or the release script. This is based off the same logic that the ScriptManager control uses. If the HttpContext.IsDebuggingEnabled is set to true then we will render the debug script. There are a couple ways in which the IsDebuggingEnabled is set to false all of which can be found on MSDN.

Hopefully this makes the script helper a little more useful. I would love to continue to improve these and maybe even bring them into the main framework some day so please let me know what you think! In fact, this improvement was a request on my original post. You can download MVC Futures here.