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 ...
随机推荐
- LIS问题(DP解法)---poj1631
题目链接:http://poj.org/problem?id=1631 这个题题目有些难看懂hhh,但实质就是求LIS--longest increasing sequence. 以下介绍LIS的解法 ...
- 3.Longest Substring Without Repeating Characters(string; HashTable)
Given a string, find the length of the longest substring without repeating characters. For example, ...
- python 安装scikit!!!
首先,吐槽一下,真的是折腾好几天,一会更新这个,一会更新那个,总是各种奇葩问题诸如此类: cannot import check-build pip有新版本,需要更新(黄字) 其中scipy出错最多, ...
- runloop - CFRunLoopObserverRef
1.
- 一种管理z-index属性的方案
原文在这里:http://www.smashingmagazine.com/2014/06/12/sassy-z-index-management-for-complex-layouts/ 核心思想是 ...
- struts框架问题六之从值栈中获取值
6. 问题六: 在JSP中获取值栈的数据 * 总结几个小问题: > 访问root中数据 不需要# > 访问context其它对象数据 加 # > 如果向root中存入对象的话,优先使 ...
- db2 统计信息 runstats
1.runstats的语法:runstats on table [模式名].[表名] with distribution and detailed indexes all注意:你可以在所有列上,或者仅 ...
- pycharm中的常用快捷键
查找 Ctrl + F 替换 Ctrl + R 注释 Ctrl + / 去掉注释 Ctrl + / Function Shortcut Use this shortcut to... Clos ...
- 一名优秀的UI设计师应该具备哪些条件?
想做好一个好的UI设计师除了应该具有一定的审美能力,还要了解整个产品的开发过程,因为目前国内的软件行业还不能对UI设计形成应有的重视度,所以对我们的要求就更高了,你要能作出夺人眼球的东西,还要站在用户 ...
- datatables分页
一万条以下 var dataTables = $('#dataTables').DataTable(); 一万条以上 var dataTables = $('#dataTables').DataTab ...