扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
Resource Name | DEBUG Uncompressed | DEBUG Compressed | Release Uncompressed | Release Compressed |
MicrosoftAjax.js | 260,705bytes | 44,512 bytes | 84,337 bytes | 23,765 bytes |
MicrosoftAj 在ASP.NET中上传图片并生成缩略图的C#源码 <FONT size=4><FONT size=4><FONT size=4>using System;using System.Collections。axWebForms.js | 66,186 bytes | 11,718 bytes | 29,841 bytes | 7,839 bytes |
上表的数据我就不再多说了,一目了然。那么由谁来决定是使用Debug版本,还是Release版本。你可以设置ScriptManager的ScriptMode属性,也可以修改web.config的compilation节点的deubg属性。
现在我们再来讨论一下,该如何实现脚本的压缩的?其实,这个也是相当简单的,从ASP.NET AJAX给我们提供的默认配合节点中,可以非常明显的看到这样一段代码:
<scriptResourceHandler enableCompression="true" enableCaching="true" /> |
但是,事实完全就是这样吗?未必!我想说的是,这里的压缩并不适用于低于IE7的浏览器,包括IE6。有人肯定不信了,IE6可是绝大多数据普通用户使用的浏览器,这个都不支持,那这个功能还有什么意义啊!您还真别不信,事实就是这样。我们先来看一段代码,然后再来看一个例子。
直接找到RuntimeScriptResourceHandler类,它实现了IScriptResourceHandler 该接口只有一个方法GetScriptResourceUrl顾名思义它就是获取访问脚本资源的URL地址,在RuntimeScriptResourceHandler中,它的实现是这样的:
string IScriptResourceHandler.GetScriptResourceUrl(Assembly assembly, string resourceName, CultureInfo culture, bool zip, bool notifyScriptLoaded) { if (!ScriptResourceHandler.IsCompressionEnabled(HttpContext.Current)) { zip = false; } Tuple tuple = new Tuple(new object[] { assembly, resourceName, culture, zip, notifyScriptLoaded } ); string text = (string) _urlCache[tuple]; if (text == null) { string name; ScriptResourceHandler.ScriptResourceInfo instance = ScriptResourceHandler.ScriptResourceInfo.GetInstance(assembly, resourceName); if (instance == ScriptResourceHandler.ScriptResourceInfo.Empty) { ThrowUnknownResource(resourceName); } Stream manifestResourceStream = assembly.GetManifestResourceStream(instance.ScriptName); if ((manifestResourceStream == null) || (manifestResourceStream.ReadByte() == -1)) { ThrowUnknownResource(resourceName); } culture = ScriptResourceHandler.DetermineNearestAvailableCulture(assembly, resourceName, culture); Pair<AssemblyName, DateTime> assemblyInfo = ScriptResourceHandler.GetAssemblyInfo(assembly); AssemblyName first = assemblyInfo.First; DateTime second = assemblyInfo.Second; if (assembly.GlobalAssemblyCache) { StringBuilder builder = new StringBuilder(); builder.Append(first.Name); builder.Append(','); builder.Append(first.Version); builder.Append(','); if (first.CultureInfo != null) { builder.Append(first.CultureInfo); } builder.Append(','); builder.Append(HexParser.ToString(first.GetPublicKeyToken())); name = builder.ToString(); } else { name = first.Name; } if (_absoluteScriptResourceUrl == null) { _absoluteScriptResourceUrl = VirtualPathUtility.ToAbsolute("~/ScriptResource.axd"); } text = string.Concat(new object[] { _absoluteScriptResourceUrl, "?d=", ScriptResourceHandler.EncryptString((zip ? (notifyScriptLoaded ? "Z" : "z") : (notifyScriptLoaded ? "U" : "u")) + name + "|" + resourceName + "|" + culture.ToString()), "&t=", second.Ticks } ); _urlCache[tuple] = text; } return text; } code 1 |
如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。
现场直击|2021世界人工智能大会
直击5G创新地带,就在2021MWC上海
5G已至 转型当时——服务提供商如何把握转型的绝佳时机
寻找自己的Flag
华为开发者大会2020(Cloud)- 科技行者