Using MvcContrib ScriptInclude, Stylesheet, And T4MVC

8. October 2009

I am always looking for more ways I can integrate features of MVC Contrib into my ASP.NET MVC projects. I also have started using David Ebbo’s T4MVC Template that generates strongly typed helpers for ASP.NET MVC (download).

Before I integrated these tools my script and style includes looked like…

<script src="../../Content/Scripts/jquery-1.3.2.min.js" type="text/javascript"></script>
<script src="../../Content/Scripts/jquery-ui-1.7.2.custom.min.js" type="text/javascript"></script>

<link href="../../Content/Styles/Site.css" rel="stylesheet" type="text/css" />
<link href="../../Content/Styles/start/jquery-ui-1.7.2.custom.css" rel="stylesheet" type="text/css" />

I remembered hearing about ScriptInclude and StyleInclude Html Helpers in the MVC Contrib so I updated the above references to the following…

<%= Html.ScriptInclude("~/Content/Scripts/jquery-1.3.2.min.js")%>
<%= Html.ScriptInclude("~/Content/Scripts/jquery-ui-1.7.2.custom.min.js")%>
    
<%= Html.Stylesheet("~/Content/Styles/Site.css")%>
<%= Html.Stylesheet("~/Content/Styles/start/jquery-ui-1.7.2.custom.css")%>

I was please about using the MVC Contrib Helpers, but I wasn’t thrilled with having hard-coded strings laying around which is where the T4MVC Template comes into play.

There are many features of the T4MVC Template (many more than I planned on covering today), but one of them is interrogating your project structure and generating static classes with read-only references to your Scripts, Styles, and Images.

So, after running the T4MVC Template, I updated my references to the following…

<%= Html.ScriptInclude(Links.Content.Scripts.jquery_1_3_2_min_js)%>
<%= Html.ScriptInclude(Links.Content.Scripts.jquery_ui_1_7_2_custom_min_js)%>
    
<%= Html.Stylesheet(Links.Content.Styles.Site_css)%>
<%= Html.Stylesheet(Links.Content.Styles.start.jquery_ui_1_7_2_custom_css)%>

Looks pretty good, doesn't it? Well, there is only one problem… it doesn’t work! Why? Well, the output of the T4MVC Links are relative paths that have been resolved (meaning they no longer have the “~”). The MVC Contrib Helpers assume that if the URL passed it it doesn’t have the “~”, then it will prepend either “~/Scripts/” for scripts or “~/content/css/'” for styles.

Seeing that I have moved my scripts, styles, and images under the “~/Content” folder, there are a couple of changes to the MVC Contrib Html Helpers that could make this work…

    1. Provide some sort of mechanism to define the paths prepended to the Scripts and Styles if there is no “~”
    2. Override the Html Helpers with another option to not prepend any path information
    3. Possibly search for the “/” instead of the “~” when determining if a path should be prepended to the URL
    Can you think of any other solutions to get these to play well together?

Blog ,


Comments

Eric Hexter
Eric Hexter
10/8/2009 6:56:42 PM #
Sounds like it is time to create a fork and contribute back to MvcContrib!!!!  
10/8/2009 7:52:55 PM #
Using MvcContrib ScriptInclude, Stylesheet, And T4MVC

You've been kicked (a good thing) - Trackback from DotNetKicks.com
10/8/2009 7:59:00 PM #
Yes, I think you are right... it's about time to give back to an open source project! Now, I need to figure out how the who Git forking stuff works ;)
10/9/2009 2:07:20 AM #
Very nice and clean...
10/9/2009 7:26:28 AM #
Nice post. May be the ScriptInclude and StyleSheet extension methods should have an overload which can specify if there is any change in the default path or folder structure.

Building an inbuilt search capability to resolve paths also looks sensible. It can be similar to the way ViewEngine resolves path for view pages.
10/9/2009 9:42:28 PM #
Использование MvcContrib ScriptInclude, Stylesheet и T4MVC

Thank you for submitting this cool story - Trackback from progg.ru
10/10/2009 1:15:39 PM #
Daily tech links for .net and related technologies - October 9-11, 2009

Daily tech links for .net and related technologies - October 9-11, 2009 Web Development Using MvcContrib
11/21/2009 10:25:51 AM #
I bookmarked your post will read this latter


Regards

Arvin

Add comment




  Country flag

biuquote
  • Comment
  • Preview
Loading