gpt4 book ai didi

asp.net - 在代码中访问 ScriptManager 代理

转载 作者:行者123 更新时间:2023-11-30 10:56:11 25 4
gpt4 key购买 nike

我有一种情况,我想在我的 ScriptManager 中引用的 js 文件(例如“custom.js?2009082020091417”)(包含在我的母版页中)的路径中添加“最后修改”时间戳以及任何 ScriptManagerProxies(内容页面)。

我可以轻松地访问代码中的 ScriptManager,然后遍历它的脚本集合以获取我已声明设置的脚本路径,然后“设置”一个带有“?[lastmodifiedtimestamp]”的新路径。

问题是,我不知道如何访问任何可能存在的 ScriptManagerProxies。

调试时,我可以在非公共(public)成员(._proxies)中看到代理。我查看了文档,但看不到您实际上可以在何处公开访问此集合。

我是不是漏了什么?

我的内容页面的 Page_PreRenderComplete 事件的基类中有以下代码:

ScriptManager sm = ScriptManager.GetCurrent((Page)this);
if(sm != null)
{
foreach (ScriptReference sr in sm.Scripts)
{
string fullpath = Server.MapPath(sr.Path);
sr.PathWithVersion(fullpath); //extension method that sets "new" script path
}
}

上面的代码提供了我在 MasterPage 中定义的一个脚本,但没有提供我在内容页面的 ScriptManagerProxy 中定义的另外两个脚本。

最佳答案

想出了一个解决方案。似乎可以访问所有合并脚本的唯一位置是在主 ScriptManager 的 ResolveScriptReference 事件中。在这种情况下,对于每个具有定义路径的脚本,我使用一种扩展方法,该方法将根据 js 文件的最后修改日期添加“版本号”。现在我的 js 文件是“版本化的”,当我对 js 文件进行更改时,浏览器将不会缓存旧版本。

母版页代码:

 protected void scriptManager_ResolveScriptReference(object sender, ScriptReferenceEventArgs e)
{
if (!String.IsNullOrEmpty(e.Script.Path))
{
e.AddVersionToScriptPath(Server.MapPath(e.Script.Path));
}
}

扩展方法:

 public static void AddVersionToScriptPath(this ScriptReferenceEventArgs scrArg, string fullpath)
{
string scriptpath = scrArg.Script.Path;

if (File.Exists(fullpath))
{
FileInfo fi = new FileInfo(fullpath);
scriptpath += "?" + fi.LastWriteTime.ToString("yyyyMMddhhmm");
}

scrArg.Script.Path = scriptpath;
}

关于asp.net - 在代码中访问 ScriptManager 代理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1308032/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com