上表的数据我就不再多说了,一目了然。那么由谁来决定是使用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