seajs的动态压缩

base: '/Bundle/js/',
comboSyntax: ['?', ','],
当运行seajs配置问题后,会启动响请求,Bundle控制器,去压缩js文件


#region js压缩合并
        class CacheItem
        {
            public string Content { set; get; }
            public DateTime Expires { set; get; }
        }
        [HttpGet]
        [IsPostedFromThisSite]
        [CacheFilter(CacheProfile = "DefaultLong")]
        public ActionResult js(string path, string v)
        {
            string tail = Request.QueryString[null];
            string cacheKey = path + tail;
            CacheItem cacheItem = HttpRuntime.Cache.Get(cacheKey) as CacheItem;//服务端缓存
            if (cacheItem == null)
            {
                path = "~/Scripts/" + path;
                StringBuilder notMinifiedJs = new StringBuilder();
                string tempfile = Server.MapPath(path);
                if (IO.File.Exists(tempfile))
                {
                    notMinifiedJs.Append(IO.File.ReadAllText(tempfile, Encoding.UTF8));
                }
                else
                {
                    if (tail != null)
                    {
                        foreach (var item in tail.Split(new string[] { ",", ";", "|" }, StringSplitOptions.RemoveEmptyEntries))
                        {
                            string temppath = Server.MapPath(path + item);
                            if (IO.File.Exists(temppath))
                            {
                                notMinifiedJs.Append(IO.File.ReadAllText(temppath, Encoding.UTF8));
                            }
                        }
                    }
                }
                var minifier = new Minifier();
                var minifiedJs = minifier.MinifyJavaScript(notMinifiedJs.ToString(), new CodeSettings
                {
                    EvalTreatment = EvalTreatment.MakeImmediateSafe,
                    PreserveImportantComments = false
                });
#if !DEBUG
                cacheItem = new CacheItem() { Content = minifiedJs, Expires = DateTime.Now.AddDays(1) };
                HttpRuntime.Cache.Insert(cacheKey, cacheItem, null, cacheItem.Expires, TimeSpan.Zero);
#endif
                return JavaScript(minifiedJs);
            }
            else
            {
                return JavaScript(cacheItem.Content);
            }
        }
        #endregion
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值