1

ScriptManager (私の MasterPage に含まれる) および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 で定義した 1 つのスクリプトを提供しますが、コンテンツ ページの ScriptManagerProxy で定義した他の 2 つのスクリプトは提供しません。

4

1 に答える 1

4

解決策を思いつきました。マージされたすべてのスクリプトにアクセスできる唯一の場所は、メインの 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;
 }
于 2009-08-21T14:51:23.357 に答える