Click here to Skip to main content
15,867,835 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 51.9K   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;
using System.Web;

namespace Combres
{
    /// <summary>
    /// Utility class providing methods to convert from relative ASP.NET URLs to absolute URLs.
    /// Refactored from original code at http://www.west-wind.com/Weblog/posts/154812.aspx.
    /// </summary>
    internal static class UriUtils
    {
        /// <summary>
        /// Returns an absolute URL from a relative server path starting with ~ character.
        /// </summary>
        public static string ToAbsoluteUrl(this string serverUrl)
        {
            return ToAbsoluteUrl(serverUrl, false);
        }

        /// <summary>
        /// Returns an absolute URL from a relative server path starting with ~ character.
        /// If <paramref name="forceHttps"/> is true, return HTTPS URL.
        /// </summary>
        public static string ToAbsoluteUrl(this string serverUrl, bool forceHttps)
        {
            if (string.IsNullOrEmpty(serverUrl))
                return serverUrl;
            if (IsAbsoluteUrl(serverUrl))
                return serverUrl;

            var adjustedServerUrl = ResolveUrl(serverUrl);
            var result = new Uri(HttpContext.Current.Request.Url, adjustedServerUrl);
            return forceHttps
                ? result.ToHttps().ToString()
                : result.ToString();

        }

        /// <summary>
        /// Returns the relative HTTP path from a partial path starting out with a ~ character.
        /// </summary>
        public static string ResolveUrl(this string originalUrl)
        {
            if (string.IsNullOrEmpty(originalUrl) ||
                    IsAbsoluteUrl(originalUrl) ||
                    !originalUrl.StartsWith("~"))
                return originalUrl;
            
            /* 
             * Fix up path for ~ root app dir directory
             * VirtualPathUtility blows up if there is a 
             * query string, so we have to account for this.
             */
            var queryStringStartIndex = originalUrl.IndexOf('?');
            if (queryStringStartIndex != -1) 
            {
                var baseUrl = originalUrl.Substring(0, queryStringStartIndex);
                var queryString = originalUrl.Substring(queryStringStartIndex);
                return string.Concat(
                    VirtualPathUtility.ToAbsolute(baseUrl),
                    queryString);
            }

            return VirtualPathUtility.ToAbsolute(originalUrl);
        }

        private static Uri ToHttps(this Uri uri)
        {
            var builder = new UriBuilder(uri)
                              {
                                  Scheme = Uri.UriSchemeHttps, 
                                  Port = 443
                              };
            return builder.Uri;
        }

        private static bool IsAbsoluteUrl(string url)
        {
            int indexOfSlashes = url.IndexOf("://");
            int indexOfQuestionMarks = url.IndexOf("?");

            /*
             * This has :// but still NOT an absolute path:
             * ~/path/to/page.aspx?returnurl=http://www.my.page
             */
            return indexOfSlashes > -1 
                && (indexOfQuestionMarks < 0 || indexOfQuestionMarks > indexOfSlashes);
        }
    }
}

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