由于捆绑压缩会对所有包含的文件进行压缩,无法设置忽略对某个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 脚本文件捆绑压缩 导致 脚本出错的问题的更多相关文章

  1. [代码示例]用Fine Uploader+ASP.NET MVC实现ajax文件上传

    原文 [代码示例]用Fine Uploader+ASP.NET MVC实现ajax文件上传 Fine Uploader(http://fineuploader.com/)是一个实现 ajax 上传文件 ...

  2. ASP.NET MVC 导出CSV文件

    ASP.NET MVC   导出CSV文件.直接贴代码 /// <summary> /// ASP.NET MVC导出CSV文件Demo1 /// </summary> /// ...

  3. asp.net Mvc 模型绑定项目过多会导致页面运行时间卡

    asp.net Mvc 模型绑定项目过多会导致页面运行时间卡的问题. 解决方式就是采用ModelView方式进行精简,已减少模型绑定及验证的时间.

  4. asp.net mvc 应用Bundle(捆绑和微小)压缩技术 启用 BundleConfig 配置web.config

    从MVC4开始,我们就发现,项目中对Global.asax进行了优化,将原来在MVC3中使用的代码移到了 [App_Start]文件夹下,而Global.asax只负责初始化.其中的BundleCon ...

  5. Asp.Net MVC 文件管理Demo(文件展示,上传,下载,压缩,文件重命名等)

    之前 ,有想做一个文件管理页面. 参考了 许多资料,终于完成了一个基于Asp.net MVC 的文件管理Demo.界面如下.   一,实现功能及相关技术 文件管理Demo基于Asp.NET MVC , ...

  6. ASP.NET MVC 静态资源打包和压缩问题小记

    ASP.NET MVC 中有个 BundleConfig 用于静态资源的打包和压缩,我在使用的过程中遇到一些问题,现在做下总结,并给出具体的解决方案. 问题一:打包压缩后的 JavaScript 和 ...

  7. ASP.NET MVC实现Excel文件的上传下载

    在应用系统开发当中,文件的上传和下载是非常普遍的需求.在基于.NET的C/S架构的项目开发当中,有多种方案可以实现文件的上传和下载(httpwebrequest.webclient等),而且多采用异步 ...

  8. Asp.Net MVC 3.0 使用Gzip压缩

    前言 Gzip最早由Jean-loup Gailly和Mark Adler创建,用于Unix系统的文件压缩.我们在Linux中经常会用到后缀为.gz的文件,它们就是Gzip格式的.现今已经成为Inte ...

  9. [Fine Uploader] 用Fine Uploader+ASP.NET MVC实现ajax文件上传[代码示例]

    Fine Uploader(http://fineuploader.com/)是一个实现 ajax 上传文件的 Javascript 组件   This project attempts to ach ...

随机推荐

  1. cf-Round541-Div2-F(并查集+静态链表)

    题目链接:http://codeforces.com/contest/1131/problem/F 思路: 很容易看出这是一道并查集的题目,因为要输出每个cage中住的鸟的编号,故采用静态链表.用l[ ...

  2. 【校招面试 之 C/C++】第8题 C++中的静态绑定与动态绑定

    转自:https://blog.csdn.net/chgaowei/article/details/6427731   做了部分修改 为了支持c++的多态性,才用了动态绑定和静态绑定.理解他们的区别有 ...

  3. OC 线程操作 - GCD使用 -线程通讯, 延迟函数和一次性代码

    - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{ // [self downImag ...

  4. php5.3 延迟静态绑定 static关键字

    //传统模式 --这段代码能很好工作,但大量的重复代码很烦人,不想为每个DomainObject子类都创建这段相同代码吧? /* abstract class DomainObject{} class ...

  5. MyEclipse2018.9.0设置全局编码

    1.windows->Preferences打开"首选项"对话框,左侧导航,导航到general->Workspace 右侧Text file encoding,选择O ...

  6. 使用bcp工具对boost库裁剪

    有些时候,我们需要通过源代码来发布我们的产品,在使用了CI工具之后,一般我们要求每天对源码进行构建,以防止代码不可用了还不自知.如果我们使用了Boost库,我们就需要在构建的过程中将Boost同时构建 ...

  7. 前端之JavaScript笔记1

    一 JavaScript的引入方式 <!DOCTYPE html> <html lang="en"> <head> <meta chars ...

  8. Linux必须学的东西,鉴于各大公司实际开发都不用Windows系统

    Windows安全性比较差,所以各大公司会使用其他的平台,所以像Linux就是很常用的,基于Unix的开源系统,鉴于很多人写的很散,所以自己总结下对于自己有用的重点,现在总结下简单的linxu的命令使 ...

  9. UVa 10269 Adventure of Super Mario (Floyd + DP + BFS)

    题意:有A个村庄,B个城市,m条边,从起点到终点,找一条最短路径.但是,有一种工具可以使人不费力的移动L个长度,但始末点必须是城市或村庄.这种工具有k个,每个只能使用一次,并且在城市内部不可使用,但在 ...

  10. node.js初步总结

    一:先上一段代码 process.argv.forEach(function (val, index, array) {    console.log(index + ":" + ...