AutoBundleWeb

using System;
using System.Web;
using System.Web.Optimization;

namespace AutoBundleWeb
{
    public static class ScriptsBundle2
    {
        public static IHtmlString Bundle2(params string[] paths)
        {
            string path = RegisterBundles(paths);
            return Scripts.RenderFormat(Scripts.DefaultTagFormat, path);
        }

        private static string RegisterBundles(params string[] paths)
        {
            var key = Math.Abs(string.Join(null, paths).GetHashCode());
            string virtualPath = string.Format("~/bundles/" + key);

            var bundle = BundleTable.Bundles.GetBundleFor(virtualPath);
            if (bundle == null)
            {
                var scriptBundle = new ScriptBundle(virtualPath);
                scriptBundle.Include(paths);
                BundleTable.Bundles.Add(scriptBundle);
            }

            return virtualPath;
        }
    }
}
    @ScriptsBundle2.Bundle2("~/Scripts/bootstrap.js", "~/Scripts/respond.js")

demo download

 
时间: 2024-08-13 07:36:59

AutoBundleWeb的相关文章