ScriptHelperFor MVC and WebForms projects

Posted by: Glavs Blog, on 18 Nov 2010 | View original | Bookmarked: 0 time(s)

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

Category: C# | Other Posts: View all posts by this blogger | Report as irrelevant | View bloggers stats | Views: 1190 | Hits: 18

Similar Posts

  • MvcContrib working on Portable Areas more
  • Video: DevExpress Plans For ASP.NET MVC more
  • MvcContrib version control has moved to GitHub more
  • ASP.NET MVC 2.0 and VS 2010 plan now public more
  • Testability and TDD are not reasons to use the ASP.NET MVC Framework more
  • Testability and TDD are not reasons to use ASP.NET MVC more
  • You should NOT use ASP.NET MVC if. . . more
  • Is ASP.NET MVC a half-baked solution? more
  • MvcContrib v1.0 Released! Download now more
  • New class: ASP.NET MVC Boot Camp developer training more

News Categories

.NET | Agile | Ajax | Architecture | ASP.NET | BizTalk | C# | Certification | Data | DataGrid | DataSet | Debugger | DotNetNuke | Events | GridView | IIS | Indigo | JavaScript | Mobile | Mono | Patterns and Practices | Performance | Podcast | Refactor | Regex | Security | Sharepoint | Silverlight | Smart Client Applications | Software | SQL | VB.NET | Visual Studio | W3 | WCF | WinFx | WPF | WSE | XAML | XLinq | XML | XSD