我在反射器中挖了一会儿,不幸的是,这似乎是一件棘手的事情。MS 没有提供我能找到的任何好的扩展点。但是,如果您愿意使用一个不错的反射技巧,那么为 ScriptManager 添加以下 ControlAdapter 应该可以解决问题:
public class VersionedScriptManagerAdapter : ControlAdapter
{
protected new ScriptManager Control
{
get { return (ScriptManager) base.Control; }
}
protected override void OnPreRender(System.EventArgs e)
{
base.OnPreRender(e);
var compositeScriptField = Control.GetType().GetField("_compositeScript",
BindingFlags.NonPublic | BindingFlags.Instance);
var currentCompositeScript = Control.CompositeScript;
var versionedCompositeScript = new VersionedCompositeScriptReference();
CopyCompositeScript(currentCompositeScript, versionedCompositeScript);
compositeScriptField.SetValue(Control, versionedCompositeScript);
}
private void CopyCompositeScript(CompositeScriptReference sourceCompositeScript, CompositeScriptReference targetCompositeScript)
{
targetCompositeScript.Path = sourceCompositeScript.Path;
targetCompositeScript.ResourceUICultures = sourceCompositeScript.ResourceUICultures;
targetCompositeScript.ScriptMode = sourceCompositeScript.ScriptMode;
foreach (var scriptReference in sourceCompositeScript.Scripts)
{
targetCompositeScript.Scripts.Add(scriptReference);
}
}
private class VersionedCompositeScriptReference : CompositeScriptReference
{
protected override string GetUrl(ScriptManager scriptManager, bool zip)
{
string version = ConfigurationManager.AppSettings["ScriptVersion"];
return base.GetUrl(scriptManager, zip) + "&v=" + version;
}
}
}
然后要连接这个控制适配器,您需要创建一个 Web.browser 文件并将其放在网站上的 App_Browsers 文件夹中。Web.browser 文件应如下所示:
<browsers>
<browser refID="Default">
<controlAdapters>
<adapter controlType="System.Web.UI.ScriptManager"
adapterType="MyNamespace.VersionedScriptManagerAdapter">
</adapter>
</controlAdapters>
</browser>
</browsers>
我对此进行了测试,它对我有用。我希望它对你有帮助。