public Bundle GetBundleFor(string bundleVirtualPath)
        {
            Exception error = ExceptionUtil.ValidateVirtualPath(bundleVirtualPath, "bundleVirtualPath");

            if (error != null)
            {
                throw error;
            }

            // Search for exact path match first
            if (StaticBundles.ContainsKey(bundleVirtualPath))
            {
                return(StaticBundles[bundleVirtualPath]);
            }

            if (DynamicBundles.Count > 0)
            {
                // Otherwise look at virtualPath component for a bundle extension match
                bundleVirtualPath = bundleVirtualPath.Replace("\\", "/");
                int index = bundleVirtualPath.LastIndexOf("/", StringComparison.Ordinal);
                // Note: virtualPath always starts with ~/ so should be never -1;
                string last = bundleVirtualPath.Substring(index + 1);

                if (DynamicBundles.ContainsKey(last))
                {
                    return(DynamicBundles[last]);
                }
            }
            return(null);
        }
        /// <summary>
        /// Adds a bundle to the collection
        /// </summary>
        /// <param name="bundle">The bundle to add to the collection</param>
        public void Add(Bundle bundle)
        {
            if (bundle == null)
            {
                throw new ArgumentNullException("bundle");
            }

            string bundlePath = bundle.Path;
            Bundle oldBundle  = null;

            // REVIEW: Should we lock down when bundles can be registered?
            DynamicFolderBundle db = bundle as DynamicFolderBundle;

            if (db != null)
            {
                // Remember the old bundle so we can invalidate the cache
                // NOTE: DynamicBundle paths are not virtual paths so we cannot use GetBundleFor
                if (DynamicBundles.ContainsKey(bundlePath))
                {
                    oldBundle = DynamicBundles[bundlePath];
                }

                DynamicBundles[bundlePath] = db;
            }
            else
            {
                // We need to resolve the url since the old bundle could have been a dynamic bundle
                oldBundle = GetBundleFor(bundlePath);

                StaticBundles[bundlePath] = bundle;
            }

            // Invalidate any old cache entries if there was a bundle already registered
            if (oldBundle != null)
            {
                oldBundle.InvalidateCacheEntries();
            }

            _bundles[bundlePath] = bundle;
        }
        /// <summary>
        /// Removes a single bundle from the collection.
        /// </summary>
        /// <param name="bundle">The bundle to remove from the collection.</param>
        /// <returns>A boolean value indicating whether the bundle was succesfully removed from the collection.</returns>
        public bool Remove(Bundle bundle)
        {
            if (bundle == null)
            {
                throw new ArgumentNullException("bundle");
            }

            bool wasRemoved = _bundles.Remove(bundle.Path);

            if (wasRemoved)
            {
                if (bundle is DynamicFolderBundle)
                {
                    DynamicBundles.Remove(bundle.Path);
                }
                else
                {
                    StaticBundles.Remove(bundle.Path);
                }
            }
            return(wasRemoved);
        }
 /// <summary>
 /// Removes all the bundles from the collection.
 /// </summary>
 public void Clear()
 {
     _bundles.Clear();
     DynamicBundles.Clear();
     StaticBundles.Clear();
 }