Asp.Net Mvc ScriptBundle 脚本文件捆绑压缩 导致 脚本出错的问题
由于捆绑压缩会对所有包含的文件进行压缩,无法设置忽略对某个js文件的压缩。导致压缩该js后,脚本出错的问题。
解决方式:
重写 ScriptBundle 的 GenerateBundleResponse 。代码如下
public class ScriptBundleFileIgnoreZip: ScriptBundle
{ readonly HashSet<string> bundles = new HashSet<string>(); /// <summary>
/// 构造函数
/// </summary>
/// <param name="virtualPath"></param>
public ScriptBundleFileIgnoreZip(string virtualPath) : base(virtualPath) { } /// <summary>
/// 忽略压缩包含文件
/// </summary>
/// <param name="virtualPaths">文件组</param>
/// <returns></returns>
public Bundle IgnoreMinInclude(params string[] virtualPaths)
{
foreach (string path in virtualPaths)
{
string cPath = path.TrimStart('~');
if (!bundles.Contains(cPath))
{
bundles.Add(cPath);
}
} return base.Include(virtualPaths);
} /// <summary>
/// 生成压缩捆绑响应文
/// </summary>
/// <param name="context">捆绑上下文</param>
/// <returns></returns>
public override BundleResponse GenerateBundleResponse(BundleContext context)
{
if (context == null)
{
throw new ArgumentNullException("context");
}
IEnumerable<BundleFile> enumerable = this.EnumerateFiles(context);
enumerable = context.BundleCollection.IgnoreList.FilterIgnoredFiles(context, enumerable);
enumerable = this.Orderer.OrderFiles(context, enumerable);
if (this.EnableFileExtensionReplacements)
{
enumerable = context.BundleCollection.FileExtensionReplacementList.ReplaceFileExtensions(context, enumerable);
} StringBuilder bundleContent = new StringBuilder();
string text2 = ";" + Environment.NewLine;
Microsoft.Ajax.Utilities.Minifier minifier = new Microsoft.Ajax.Utilities.Minifier();
foreach (var bf in enumerable)
{
if (bundles.Contains(bf.VirtualFile.VirtualPath))
{
bundleContent.Append(bf.ApplyTransforms());
}
else
{
bundleContent.Append(minifier.MinifyJavaScript(bf.ApplyTransforms(), new Microsoft.Ajax.Utilities.CodeSettings()
{
EvalTreatment = Microsoft.Ajax.Utilities.EvalTreatment.MakeImmediateSafe,
PreserveImportantComments = false
}));
} bundleContent.Append(text2);
} if (this.Transforms != null)
{
this.Transforms.Clear();
} return this.ApplyTransforms(context, bundleContent.ToString(), enumerable); }
用法:
var scriptBundles = new ScriptBundleFileIgnoreZip("~/JS");
scriptBundles.Include("~/要压缩的文件0.js");
scriptBundles.IgnoreMinInclude("~/忽略压缩的文件.js")
.Include("~/要压缩的文件1.js", "~/要压缩的文件2.js");
BundleTable.Bundles.Add(scriptBundles);
Asp.Net Mvc ScriptBundle 脚本文件捆绑压缩 导致 脚本出错的问题的更多相关文章
- [代码示例]用Fine Uploader+ASP.NET MVC实现ajax文件上传
原文 [代码示例]用Fine Uploader+ASP.NET MVC实现ajax文件上传 Fine Uploader(http://fineuploader.com/)是一个实现 ajax 上传文件 ...
- ASP.NET MVC 导出CSV文件
ASP.NET MVC 导出CSV文件.直接贴代码 /// <summary> /// ASP.NET MVC导出CSV文件Demo1 /// </summary> /// ...
- asp.net Mvc 模型绑定项目过多会导致页面运行时间卡
asp.net Mvc 模型绑定项目过多会导致页面运行时间卡的问题. 解决方式就是采用ModelView方式进行精简,已减少模型绑定及验证的时间.
- asp.net mvc 应用Bundle(捆绑和微小)压缩技术 启用 BundleConfig 配置web.config
从MVC4开始,我们就发现,项目中对Global.asax进行了优化,将原来在MVC3中使用的代码移到了 [App_Start]文件夹下,而Global.asax只负责初始化.其中的BundleCon ...
- Asp.Net MVC 文件管理Demo(文件展示,上传,下载,压缩,文件重命名等)
之前 ,有想做一个文件管理页面. 参考了 许多资料,终于完成了一个基于Asp.net MVC 的文件管理Demo.界面如下. 一,实现功能及相关技术 文件管理Demo基于Asp.NET MVC , ...
- ASP.NET MVC 静态资源打包和压缩问题小记
ASP.NET MVC 中有个 BundleConfig 用于静态资源的打包和压缩,我在使用的过程中遇到一些问题,现在做下总结,并给出具体的解决方案. 问题一:打包压缩后的 JavaScript 和 ...
- ASP.NET MVC实现Excel文件的上传下载
在应用系统开发当中,文件的上传和下载是非常普遍的需求.在基于.NET的C/S架构的项目开发当中,有多种方案可以实现文件的上传和下载(httpwebrequest.webclient等),而且多采用异步 ...
- Asp.Net MVC 3.0 使用Gzip压缩
前言 Gzip最早由Jean-loup Gailly和Mark Adler创建,用于Unix系统的文件压缩.我们在Linux中经常会用到后缀为.gz的文件,它们就是Gzip格式的.现今已经成为Inte ...
- [Fine Uploader] 用Fine Uploader+ASP.NET MVC实现ajax文件上传[代码示例]
Fine Uploader(http://fineuploader.com/)是一个实现 ajax 上传文件的 Javascript 组件 This project attempts to ach ...
随机推荐
- cdoj841-休生伤杜景死惊开 (逆序数变形)【线段树 树状数组】
http://acm.uestc.edu.cn/#/problem/show/841 休生伤杜景死惊开 Time Limit: 3000/1000MS (Java/Others) Memory ...
- 10-string类的length()返回值一起的问题
c++ string类length()(size())函数返回值–无符号数 首先,先来发现问题 string s = ""; for(int i = 0; i < s.len ...
- python激活码
- 孤立森林(isolation forest)
1.简介 孤立森林(Isolation Forest)是另外一种高效的异常检测算法,它和随机森林类似,但每次选择划分属性和划分点(值)时都是随机的,而不是根据信息增益或者基尼指数来选择. 在建树过程中 ...
- spring mvc 默认页面
只需要在servlet.xml页面中添加如下配置: <mvc:view-controller path="/" view-name="login"/> ...
- Python GUI 编程
Python GUI编程(Tkinter) Python 提供了多个图形开发界面的库,几个常用 Python GUI 库如下: Tkinter: Tkinter 模块(Tk 接口)是 Python 的 ...
- 2018.10.18 NOIP训练 [SCOI2018]Pipi 酱的日常(线段树)
传送门 线段树好题啊. 题目要求的是sum−a−b−c+maxsum-a-b-c+maxsum−a−b−c+max{∣a+v∣+∣b+v∣+∣c+v∣|a+v|+|b+v|+|c+v|∣a+v∣+∣b ...
- 2018.09.12 poj3621Sightseeing Cows(01分数规划+spfa判环)
传送门 01分数规划板题啊. 发现就是一个最优比率环. 这个直接二分+spfa判负环就行了. 代码: #include<iostream> #include<cstdio> # ...
- 2018.07.06 POJ1556 The Doors(最短路)
The Doors Time Limit: 1000MS Memory Limit: 10000K Description You are to find the length of the shor ...
- 优秀前端工程师必备: 非常常用的checkbox的骚操作---全选和单选demo
提要: 前端开发的时候, 经常会遇到表格勾选, 单个勾选判断是否全选的事情.趁着有时间, 总结一下以备不时之需! 就像下面这个栗子: 1 源代码: h5 // 全选框 <input type=& ...