HTTP ハンドラーを作成することで、小さな問題を解決しました。この例では、DynamicClientScript.axd と呼ばれます。
アイデアを提供するために、コードからいくつかのカットを取りました。以下のコードは、標準の埋め込みリソース URL を取得し、そこからクエリ文字列を取得して、ハンドラーへのパスに追加します。
/// <summary>
/// Gets the dynamic web resource URL to reference on the page.
/// </summary>
/// <param name="type">The type of the resource.</param>
/// <param name="resourceName">Name of the resource.</param>
/// <returns>Path to the web resource.</returns>
public string GetScriptResourceUrl(Type type, string resourceName)
{
this.scriptResourceUrl = this.currentPage.ClientScript.GetWebResourceUrl(type, resourceName);
string resourceQueryString = this.scriptResourceUrl.Substring(this.scriptResourceUrl.IndexOf("d="));
DynamicScriptSessionManager sessMngr = new DynamicScriptSessionManager();
Guid paramGuid = sessMngr.StoreScriptParameters(this.Parameters);
return string.Format("/DynamicScriptResource.axd?{0}¶mGuid={1}", resourceQueryString, paramGuid.ToString());
}
/// <summary>
/// Registers the client script include.
/// </summary>
/// <param name="key">The key of the client script include to register.</param>
/// <param name="type">The type of the resource.</param>
/// <param name="resourceName">Name of the resource.</param>
public void RegisterClientScriptInclude(string key, Type type, string resourceName)
{
this.currentPage.ClientScript.RegisterClientScriptInclude(key, this.GetScriptResourceUrl(type, resourceName));
}
次に、ハンドラーはクエリ文字列を取得して、標準リソースへの URL を構築します。リソースを読み取り、各キーをディクショナリ コレクション (DynamicClientScriptParameters) 内の値に置き換えます。
paramGuid は、正しいスクリプト パラメータ コレクションを取得するために使用される識別子です。
ハンドラーは何をしますか...
public void ProcessRequest(HttpContext context)
{
string d = HttpContext.Current.Request.QueryString["d"];
string t = HttpContext.Current.Request.QueryString["t"];
string paramGuid = HttpContext.Current.Request.QueryString["paramGuid"];
string urlFormatter = "http://" + HttpContext.Current.Request.Url.Host + "/WebResource.axd?d={0}&t={1)";
// URL to resource.
string url = string.Format(urlFormatter, d, t);
string strResult = string.Empty;
WebResponse objResponse;
WebRequest objRequest = System.Net.HttpWebRequest.Create(url);
objResponse = objRequest.GetResponse();
using (StreamReader sr = new StreamReader(objResponse.GetResponseStream()))
{
strResult = sr.ReadToEnd();
// Close and clean up the StreamReader
sr.Close();
}
DynamicScriptSessionManager sessionManager = (DynamicScriptSessionManager)HttpContext.Current.Application["DynamicScriptSessionManager"];
DynamicClientScriptParameters parameters = null;
foreach (var item in sessionManager)
{
Guid guid = new Guid(paramGuid);
if (item.SessionID == guid)
{
parameters = item.DynamicScriptParameters;
}
}
foreach (var item in parameters)
{
strResult = strResult.Replace("$" + item.Key + "$", item.Value);
}
// Display results to a webpage
context.Response.Write(strResult);
}
次に、リソースを参照するコードで、次を使用します。
DynamicClientScript dcs = new DynamicClientScript(this.GetType(), "MyNamespace.MyScriptResource.js");
dcs.Parameters.Add("myParam", "myValue");
dcs.RegisterClientScriptInclude("scriptKey");
次に、スクリプト リソースに次のものが含まれているとします。
alert('$myParam$');
次のように出力されます。
alert('myValue');
私のコードは (DynamicScriptSessionManager を使用して) キャッシュも行いますが、アイデアはわかります...
乾杯