在代码中访问 ScriptManager 代理

发布于 2024-08-02 15:37:04 字数 816 浏览 5 评论 0原文

我有一种情况,我想将“上次修改”时间戳添加到我的 ScriptManager(包含在我的 MasterPage 中)和任何 js 文件(例如“custom.js?2009082020091417”)的路径中ScriptManagerProxies(内容页面)。

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

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

调试时,我可以在非公共成员(._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 中定义的其他两个脚本。

I have a situation where I'd like to add a "last modified" timestamp to the paths of my js files (ex. "custom.js?2009082020091417") that are referenced in my ScriptManager (contained in my MasterPage) and in any ScriptManagerProxies (content pages).

I can easily get access to the ScriptManager in code and then iterate through it's Scripts collection to get the Script paths that I've set declaratively and then "set" a new path with the tacked on "?[lastmodifiedtimestamp]".

The problem is, I can't figure out how to get to any ScriptManagerProxies that may exist.

When debugging, I can see the proxies in the non-public members (._proxies). I've looked through the documentation and can't see where you can actually publicly access this collection.

Am I'm missing something?

I have the following code in the base class of my content page's Page_PreRenderComplete event:

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
     }
}

The above code gives me the one script I have defined in my MasterPage, but not the two other scripts I have defined in the ScriptManagerProxy of my content page.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

╰◇生如夏花灿烂 2024-08-09 15:37:04

想出了一个解决办法。似乎唯一可以访问所有合并脚本的地方是主 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;
 }

Came up with a solution. Seems that the only place that all the merged scripts can be accessed is in the main ScriptManager's ResolveScriptReference event. In this event, for each script that has a defined path, I use an extension method that will tack on a "version number" based on the js file's last modified date. Now that my js files are "versioned", when I make a change to a js file, the browser will not cache an older version.

Master Page Code:

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

Extension Method:

 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;
 }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文