zoukankan      html  css  js  c++  java
  • AutoBundle in asp.net mvc 5

    using System.Collections.Concurrent;
    using System.Text;
    
    namespace System.Web.Optimization
    {
        public static class Script
        {
            private static readonly ConcurrentDictionary<string, string> Bundles = new ConcurrentDictionary<string, string>();
    
            public static IHtmlString Src(params string[] paths)
            {
                string bundlePath = RegisterBundles(paths);
                if (string.IsNullOrEmpty(bundlePath))
                {
                    return RenderOrignal(paths);
                }
                return Scripts.RenderFormat(Scripts.DefaultTagFormat, bundlePath);
            }
    
            private static IHtmlString RenderOrignal(params string[] paths)
            {
                StringBuilder stringBuilder = new StringBuilder();
                for (int i = 0; i < paths.Length; i++)
                {
                    string path = paths[i];
                    if (path.StartsWith("~"))
                    {
                        path = path.Substring(1);
                    }
                    stringBuilder.AppendFormat(Scripts.DefaultTagFormat, path);
                }
    
                return (IHtmlString)new HtmlString(stringBuilder.ToString());
            }
    
            private static string RegisterBundles(params string[] paths)
            {
                string key = string.Join(null, paths);
                if (Bundles.ContainsKey(key))
                {
                    string bundlePath;
                    if (Bundles.TryGetValue(key, out bundlePath))
                    {
                        return bundlePath;
                    }
                }
                else
                {
                    string bundlePath = "~/Scripts/" + Math.Abs(key.GetHashCode());
                    var bundle = BundleTable.Bundles.GetBundleFor(bundlePath);
                    if (bundle == null)
                    {
                        var scriptBundle = new ScriptBundle(bundlePath);
                        scriptBundle.Include(paths);
                        BundleTable.Bundles.Add(scriptBundle);
                    }
                    if (Bundles.TryAdd(key, bundlePath))
                    {
                        return bundlePath;
                    }
                }
                return null;
            }
        }
    }
  • 相关阅读:
    django中ckeditor富文本编辑器使用
    xadmin安装
    RabbitMQ应用示例
    windows下安装RabbitMQ
    第四章 面向对象
    第三章 模块
    git简单使用
    python中的装饰器
    Python 使用 argparse 开发命令行工具/获取命令行参数/子命令实现
    自动化运维工具 Ansible 安装、配置及使用
  • 原文地址:https://www.cnblogs.com/chinaniit/p/4731617.html
Copyright © 2011-2022 走看看