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 ...
随机推荐
- racktables 的介绍及搭建指南
Racktables RackTables称自己为一个“机架空间.IP地址.服务器.交换机.路由器等 的管理框架”.它拥有一个web界面,执行报告和配置,并管理名字服务.RackTables以PHP5 ...
- keras—神经网络CNN—MNIST手写数字识别
from keras.datasets import mnist from keras.utils import np_utils from plot_image_1 import plot_imag ...
- Spring框架的配置文件分开管理(了解)
1. 例如:在src的目录下又多创建了一个配置文件,现在是两个核心的配置文件,那么加载这两个配置文件的方式有两种! * 主配置文件中包含其他的配置文件: <import resource=&qu ...
- JTemplate学习(一)
使用模板绑定数据,可以嵌套循环 参考:http://www.doc88.com/p-6621237324128.html <!DOCTYPE html PUBLIC "-//W3C// ...
- VBA json parser[z]
http://www.ediy.co.nz/vbjson-json-parser-library-in-vb6-xidc55680.html VB-JSON: A Visual Basic 6 (VB ...
- RPM包制作方法
一.RPM介绍 RPM 前是Red Hat Package Manager 的缩写,本意是Red Hat 软件包管理,顾名思义是Red Hat 贡献出来的软件包管理:现在应为RPM Package M ...
- 配置springMVC
1.web.xml 前端控制器 配置规则:*.do: 拦截请求路径所有的后缀为.do;/* : 拦截所有, .jsp页面也会拦截; 不会使用此配置, 因为视图会无法跳转;/ : 拦截所有, .jsp页 ...
- 局域网代理通过wget下载
下载方法: wget -r -p -np -k http://ftp.loongnix.org/os/Fedora13-o32/RPMS/mipsel/ -r, --recursive(递归) ...
- Laravel 的 Events(事件) 及 Observers(观察者)
你是否听说过单一职责原则(single responsibility principle)?我希望是的.它是程序设计的基本原则之一,它基本上的意思就是,一个类有且只有一个职责.换句话说,一个类必须且只 ...
- for() 和$.each()的用法区别
一.对于数组 var arr=['姚明','易建联','张继科']; $.each(arr,function(index,value){ document.write(index+"=&qu ...