当前位置导航:炫浪网>>网络学院>>网页制作>>ASP.NET教程

ASP.NET AJAX 资源脚本压缩的秘密

欢迎进入.NET社区论坛,与200万技术人员互动交流 >>进入   从ATLAS到正式的ASP.NET AJAX已经发生了根本性的变化了,所以当时的情况就不再多做介绍了.还是先来看看一组数据  Resource Name DEBUG Uncompressed DEBUG  Compressed  Release Uncompressed Release Compressed MicrosoftAjax.js 260,705bytes 44,512 bytes 84,337 bytes 23,765 bytes MicrosoftAjaxWebForms.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" /> 

 但是它是被注释着的,我们只需要把这个注释去掉就OK了,奇迹就这样发生了。

 但是,事实完全就是这样吗?未必!我想说的是,这里的压缩并不适用于低于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

 

相关内容
赞助商链接