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);
}
는 그럼 난 내 리소스를 참조 할 내 코드에서 나는 다음을 사용합니다. 그것 인 것처럼
이
alert('$myParam$');
이 출력됩니다 :
이 가 내 코드가 일부 캐싱합니다 (DynamicScriptSessionManager 사용)하지 않습니다하지만 당신이 얻을
alert('myValue');
DynamicClientScript dcs = new DynamicClientScript(this.GetType(), "MyNamespace.MyScriptResource.js");
dcs.Parameters.Add("myParam", "myValue");
dcs.RegisterClientScriptInclude("scriptKey");
그런 다음 내 스크립트 리소스에 포함 된 말 아이디어 ...
환호
답변 해 주셔서 감사합니다. 이것은 내가 실제로하고 싶은 것과 꽤 비슷하지만 위와 같이 스크립트 블록보다는 include가 필요합니다. – James