Click here to Skip to main content
15,886,578 members
Articles / Web Development / CSS

Combres - WebForm & MVC Client-side Resource Combine Library

Rate me:
Please Sign up or sign in to vote.
4.50/5 (8 votes)
1 Nov 2009Apache13 min read 52.1K   3.1K   22  
A .NET library which enables minification, compression, combination, and caching of JavaScript and CSS resources for ASP.NET and ASP.NET MVC web applications. Simply put, it helps your applications rank better with YSlow and PageSpeed.
#region License
// Copyright 2009 Buu Nguyen (http://www.buunguyen.net/blog)
// 
// Licensed under the Apache License, Version 2.0 (the "License"); 
// you may not use this file except in compliance with the License. 
// You may obtain a copy of the License at 
// 
// http://www.apache.org/licenses/LICENSE-2.0 
// 
// Unless required by applicable law or agreed to in writing, software 
// distributed under the License is distributed on an "AS IS" BASIS, 
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
// See the License for the specific language governing permissions and 
// limitations under the License.
// 
// The latest version of this file can be found at http://combres.codeplex.com
#endregion

using System.Web;
using System.Web.Mvc;
using System.Web.Routing;

namespace Combres
{
    /// <summary>
    /// Utility class providing extension methods for ASP.NET and ASP.NET MVC applications to
    /// work with the library.
    /// </summary>
    public static class WebExtensions
    {
        private const string UrlPattern = "{0}/{1}/{2}";

        /// <summary>
        /// Retrieves the URL of the Combres engine as registered in XML data file.
        /// </summary>
        /// <param name="routes">The actual argument is typically <code>RouteTable.Routes</code></param>
        /// <returns>The URL of the Combres engine as registered in XML data file</returns>
        public static string GetCombresUrl(this RouteCollection routes)
        {
            return Settings.Url.ResolveUrl();
        }

        /// <summary>
        /// Registers the combiner route to the <code>routes</code> collection.
        /// Invoke this in <code>Application_Start</code>.
        /// </summary>
        /// <param name="routes">The actual argument is typically <code>RouteTable.Routes</code></param>
        /// <param name="name">The route name</param>
        public static void AddCombresRoute(this RouteCollection routes, string name)
        {
            var adjustedUrl = Settings.Url.Substring(2); // Remove the "~/"
            routes.Add(name, new Route(adjustedUrl + "/{name}/{version}",
                new RouteValueDictionary(new { version = string.Empty }),
                new CombresRouteHandler()));
        }

        /// <summary>
        /// See <c>WebExtensions#CombresUrl</c>.
        /// </summary>
        public static string CombresUrl(this HtmlHelper html, string setName)
        {
            return CombresUrl(setName);
        }

        /// <summary>
        /// Generates the URL to the resource combiner for the specified <paramref name="setName"/>.
        /// </summary>
        /// <example>
        /// <![CDATA[<script type="text/javascript" src="<%= Html.CombresUrl("siteJsSet") %>"></script>]]>    
        /// </example>
        /// <param name="setName">The name of the resource set to be linked to</param>
        /// <returns>The URL to the reousrce combiner for the specified <paramref name="setName"/></returns>
        public static string CombresUrl(string setName)
        {
            var settings = Settings;
            var set = GetResourceSet(setName);
            var fullUrl = string.Format(UrlPattern, settings.Url, set.Name, set.Version);
            return fullUrl.ResolveUrl();
        }
        
        /// <summary>
        /// See <c>WebExtensions#CombresLink</c>.
        /// </summary>
        public static string CombresLink(this HtmlHelper html, string setName)
        {
            return CombresLink(setName);
        }

        /// <summary>
        /// Generates the link to the resource combiner for the specified 
        /// <paramref name="setName"/>.  If the set is a JavaScript set, 
        /// <![CDATA[<script type="text/javascript" src=...></script>]]> is returned.  
        /// If the set is a CSS set, <![CDATA[<link rel="stylesheet" type="text/css" href=... />]]> 
        /// is returned.
        /// </summary>
        /// <example>
        /// <![CDATA[<%= Html.CombresLink("bootstrapJs")%>]]>    
        /// </example>
        /// <param name="setName">The name of the resource set to be linked to</param>
        /// <returns>The link to the resource combiner for the specified <paramref name="setName"/></returns>
        public static string CombresLink(string setName)
        {
            var set = GetResourceSet(setName);
            string optimizerUrl = set.Type == ResourceType.JS ?
                "<script type=\"text/javascript\" src=\"{0}\"></script>" :
                "<link rel=\"stylesheet\" type=\"text/css\" href=\"{0}\" />";
            return string.Format(optimizerUrl, CombresUrl(set.Name));
        }

        private static ResourceSet GetResourceSet(string setName)
        {
            var set = Settings[setName];
            if (set == null)
                throw new ResourceSetNotFoundException("Set " + setName + " does not exist");
            return set;
        }

        private static Settings Settings
        {
            get
            {
                return Configuration.GetSettings(HttpContext.Current);
            }
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Apache License, Version 2.0


Written By
Chief Technology Officer KMS Technology
Vietnam Vietnam
You can visit Buu's blog at http://www.buunguyen.net/blog to read about his thoughts on software development.

Comments and Discussions