ScriptHelper–For MVC and WebForms projects

Published Thursday, November 18, 2010 4:38 PM

This issue might seem minor but I always forget the names (exact naming) or number of script files I need to get some features working in MVC or Webforms. In addition, in my applications that require a specific client side feature, I might need a series of dependent scripts to make it work. Failing to include all of them often gives ambiguous errors or the functionality or feature just doesn't work.

To that end, I have created a ScriptHelper to allow me to express those dependencies as a singular name and have all the script dependencies (or CSS) emitted for me.

The library is currently hosted on bitbucket here http://bitbucket.org/glav/mvc-script-dependency-extension

The idea is that you can express your Javascript and CSS dependencies into a separate file and pretty much call it what you want. Then, in your pages, simply the reference the dependencies by name and everything you need is pulled in for you.

The ScriptHelper I have written is a little raw, but you can define an XML file that expresses script names and its dependencies. The script helper then allows you to provide that single name and all dependent scripts are emitted in the page.

For example, in the page you can do:

<%= ScriptHelper.RequiresScript(ScriptName.jqueryValidateUnobtrusive) %>

Or

<%= ScriptHelper.RequiresScript(“jQuery-validate-unobtrusive”) %>

And you would get

<script type='text/javascript' src='/Scripts/jquery-1.4.1.js'></script>
<script type='text/javascript' src='/Scripts/jquery.validate.js'></script>
<script type='text/javascript' src='/Scripts/jquery.validate.unobtrusive.js'></script>

Additionally, you could do:

<%= ScriptHelper.RequiresScript("All") %>

And you would get the following emitted into your page:

<script type='text/javascript' src='/Scripts/jquery-1.4.1.js'></script>
<script type='text/javascript' src='/Scripts/MicrosoftAjax.js'></script>
<script type='text/javascript' src='/Scripts/MicrosoftMvcAjax.js'></script>
<script type='text/javascript' src='/Scripts/jquery.validate.js'></script>
<script type='text/javascript' src='/Scripts/jquery.validate.unobtrusive.js'></script>
<script type='text/javascript' src='/Scripts/MicrosoftMvcValidation.js'></script>

The dependency file looks something like:

<?xml version="1.0" encoding="utf-8" ?>
<Dependencies ReleaseSuffix="min" DebugSuffix="debug">
  <Dependency Name="jQuery-validate-unobtrusive" Type="js">
    <ScriptFile>~/Scripts/jquery.validate.unobtrusive.js</ScriptFile>
    <RequiredDependencies>
      <Name>jquery</Name>
      <Name>jQuery-validate</Name>
    </RequiredDependencies>
  </Dependency>

  <Dependency Name="jQuery-validate" Type="js">
    <ScriptFile>~/Scripts/jquery.validate.js</ScriptFile>
    <RequiredDependencies>
      <Name>jquery</Name>
    </RequiredDependencies>
  </Dependency>

  <Dependency Name="jQuery" Type="js">
    <ScriptFile>~/Scripts/jquery-1.4.1.js</ScriptFile>
  </Dependency>

<!— rest of dependencies …. -->

The ReleaseSuffix and DebugSuffix also act to rename a dependency file that is output to the page. These are not mandatory but it is not uncommon to host debug versions of your scripts when doing development and release/minified versions of your scripts in production. So for example, if we have a script file dependency defined as Something.js, then it will be renamed to Something.min.js in release mode and Something.debug.js in debug mode when output to the page. If not specified, the emitted dependency is left as is.

Finally, you can also specify multiple dependencies on the same line like so:

<%= ScriptHelper.RequiresScript("Microsoft-Mvc-Validation", "jQuery") %>

And all dependent scripts will be included for you without duplicating anything. Obviously the script names must be the same as what is defined in the script dependency file. I have used a static class to hold these constants and you could easily substitute your own as its just strings.

It also supports expressing CSS dependencies. For example, with the following dependency definitions:

<Dependency Name="SiteWideStyle" Type="css">
    <ScriptFile>~/Content/Site.css</ScriptFile>
  </Dependency>

  <Dependency Name="HomeStyle" Type="css">
    <ScriptFile>~/Content/Home.css</ScriptFile>
    <RequiredDependencies>
      <Name>SiteWideStyle</Name>
    </RequiredDependencies>
  </Dependency>

You can then include the following in your <head> section of your pages

<%= ScriptHelper.RequiresScript("HomeStyle") %>

And you would get

<link href='/Content/Site.css' rel='stylesheet' type='text/css' />
<link href='/Content/Home.css' rel='stylesheet' type='text/css' />

Currently, the ScriptHelper I have written supports resolving the dependency file in a few local locations, but I plan on extending it support remote locations via HTTP.

This type of thing could reduce multiple script include to a single line in a web page or view. In addition, you could have dependency files centrally located on a remote server either in the cloud or internal to your organisation, so that dependencies are always resolved via this file which can be maintained and updated as new dependencies and/or later versions of the scripts are available or required.

There is also nothing stopping you from naming your dependencies more component oriented in nature. For example you might name a dependency “AjaxGrid”, and that may load in jQuery core script, jQuery UI custom scripts, as well as some of your own custom scripts required to get this component working.

I have been using this library primarily with ASP.NET MVC projects, but it is just a set of static methods that could also be used within an ASP.NET Webforms application as well.

Feel free to leave comments/issues on the hosting site here http://bitbucket.org/glav/mvc-script-dependency-extension

Comments

# Twitter Trackbacks for ScriptHelper???For MVC and WebForms projects - Glavs Blog [asp.net] on Topsy.com said on Thursday, November 18, 2010 3:08 AM

Pingback from  Twitter Trackbacks for                 ScriptHelper???For MVC and WebForms projects - Glavs Blog         [asp.net]        on Topsy.com

# Skoda jokes: Lacrosse automotive humor – Telegraph. co. United Kingdom said on Thursday, November 18, 2010 5:09 AM

Pingback from  Skoda jokes: Lacrosse automotive humor &#8211; Telegraph. co. United Kingdom

# We Three Kings on Hang Drum said on Thursday, November 18, 2010 5:37 AM

Pingback from  We Three Kings on Hang Drum

# Avis Europe for four months the total income of the rental up? 4. 5% – Quick Facts – RTT News said on Thursday, November 18, 2010 7:32 AM

Pingback from  Avis Europe for four months the total income of the rental up? 4. 5% &#8211; Quick Facts &#8211; RTT News

# The craze Techno. What Ann?’re Cheap money? Has To Great said on Thursday, November 18, 2010 8:14 AM

Pingback from  The craze Techno. What Ann?&#8217;re Cheap money? Has To Great

# Together: Trainer and Orca said on Thursday, November 18, 2010 11:53 AM

Pingback from  Together: Trainer and Orca

# Young art skirt volume VersaEmerge in concert in the Highline Ballroom | USA Psychic Readings said on Thursday, November 18, 2010 1:45 PM

Pingback from  Young art skirt volume VersaEmerge in concert in the Highline Ballroom | USA Psychic Readings

# Concepts and r? V? At the Motor Show – AsiaOne said on Thursday, November 18, 2010 5:08 PM

Pingback from  Concepts and r? V? At the Motor Show &#8211; AsiaOne

# White Christmas The Drifters with Clyde McPhatter said on Thursday, November 18, 2010 7:35 PM

Pingback from  White Christmas The Drifters with Clyde McPhatter

# .SkywAy_World of the boundary 2011 said on Thursday, November 18, 2010 8:43 PM

Pingback from  .SkywAy_World of the boundary 2011

# Patrie Antony | Collectibles Transportation said on Thursday, November 18, 2010 9:51 PM

Pingback from  Patrie Antony | Collectibles Transportation

# LA Auto Show: the market? Automobile Am? America today said on Thursday, November 18, 2010 11:09 PM

Pingback from  LA Auto Show: the market? Automobile Am? America today

# Concepts and r? V? At the Motor Show – AsiaOne said on Friday, November 19, 2010 1:08 AM

Pingback from  Concepts and r? V? At the Motor Show &#8211; AsiaOne

# Porsche Gears Up for? Possibly taking countermeasures? The said on Friday, November 19, 2010 3:05 AM

Pingback from  Porsche Gears Up for? Possibly taking countermeasures? The

# Persuasive Writing Power | Cheap Car Supermarket said on Friday, November 19, 2010 7:42 AM

Pingback from  Persuasive Writing Power | Cheap Car Supermarket

# O? I rent an Aston Martin DB9? Toronto? said on Friday, November 19, 2010 11:25 AM

Pingback from  O? I rent an Aston Martin DB9? Toronto?

# PennyStockProphet.com -75% Comissions! Top Affiliate – $1,000 P/day! | USA Psychic Readings said on Friday, November 19, 2010 4:37 PM

Pingback from  PennyStockProphet.com -75% Comissions! Top Affiliate &#8211; $1,000 P/day! | USA Psychic Readings

# Archlord: What do you do in this play? | Escort Marketing said on Friday, November 19, 2010 9:55 PM

Pingback from  Archlord: What do you do in this play? | Escort Marketing

# Capsules MLB: Pirates to hire Hurdle as g? Monday Rant said on Saturday, November 20, 2010 6:47 AM

Pingback from  Capsules MLB: Pirates to hire Hurdle as g? Monday Rant

# All About The Porsche 365 said on Sunday, November 21, 2010 3:06 AM

Pingback from  All About The Porsche 365

# DLSR Camera - Digital Camera Comparison - Reviews, Comparisons, Buy said on Sunday, November 21, 2010 4:54 AM

Pingback from  DLSR Camera -   Digital Camera Comparison - Reviews, Comparisons, Buy

# An approach to Frye?s statement ? romance-irony-parody | USA Psychic Readings said on Sunday, November 21, 2010 6:23 AM

Pingback from  An approach to Frye?s statement ? romance-irony-parody | USA Psychic Readings

# 7 Errors in hiring a financial adviser said on Sunday, November 21, 2010 6:46 AM

Pingback from  7 Errors in hiring a financial adviser

# Best Live Person Psychic Review | USA Psychic Readings said on Sunday, November 21, 2010 7:23 AM

Pingback from  Best Live Person Psychic Review | USA Psychic Readings

# Motoring in the future – Arizona Daily Sun said on Sunday, November 21, 2010 9:09 AM

Pingback from  Motoring in the future &#8211; Arizona Daily Sun

# SLR Digital Camera - Digital Camera Comparison - Reviews, Ratings, Buy said on Sunday, November 21, 2010 10:39 AM

Pingback from  SLR Digital Camera -   Digital Camera Comparison - Reviews, Ratings, Buy

# Motoring in the future – Arizona Daily Sun said on Sunday, November 21, 2010 1:08 PM

Pingback from  Motoring in the future &#8211; Arizona Daily Sun

# Rohland said on Sunday, November 28, 2010 7:50 AM

Nice work. You might want to look at the Combres (http://combres.codeplex.com/) project on CodePlex. It exposes very similar features.

# Glav said on Sunday, November 28, 2010 9:02 PM

Thanks for the link Rohland. I had not seen Combres before and it seems quite comprehensive.

# Glavs Blog said on Friday, January 20, 2012 10:45 PM

For a while now I have been looking at different ways of managing javascript inclusion in web pages,

Leave a Comment

(required) 
(required) 
(optional)
(required) 

This Blog

Syndication