2010-03-05 3 views
3

첫째로, "수정 중"은 잘못된 용어 일 수 있습니다. 실제로 사람들이 실제로 포함 된 리소스를 수정할 수 있는지 여부를 물어 보는 온라인 사용자가 있습니다. 내가 원했던 것은 어셈블리에있는 자원을 페이지에 등록하기 전에 찾기 및 바꾸기와 같은 일종의 템플릿으로 사용하는 것입니다.페이지에 등록/참조하기 전에 포함 된 리소스를 프로그래밍 방식으로 수정하기

예 : 어셈블리에 포함 된 리소스로 몇 줄의 jQuery가 있고이 스크립트에서 프런트 엔드 프로그래머가 설정할 수있는 CSS 클래스 이름을 참조하고 있다고 가정 해보십시오. 구현 전까지 CSS 클래스가 무엇인지 알지 못하기 때문에 포함 된 리소스를 살펴보고 $ myclass $를 ThisClassName으로 바꾸는 방법이 있습니까?

도움이 될만한 것이 없다면 적어도 내 꼬리를 쫓는 것을 멈출 수 있도록 말해주십시오.

답변

1

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}&paramGuid={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"); 

그런 다음 내 스크립트 리소스에 포함 된 말 아이디어 ...

환호

0

코드가 작성되면 임베디드 리소스의 내용을 읽고 원하는대로 전환 한 다음 새 내용을 응답에 쓸 수 있습니다. 다음과 같은 내용 :

protected void Page_Load(object sender, EventArgs e) 
{ 
    string contents = ReadEmbeddedResource("ClassLibrary1", "ClassLibrary1.TestJavaScript.js"); 
    //replace part of contents 
    //write new contents to response 
    Response.Write(String.Format("<script>{0}</script>", contents)); 
} 

private string ReadEmbeddedResource(string assemblyName, string resouceName) 
{ 
    var assembly = Assembly.Load(assemblyName); 
    using (var stream = assembly.GetManifestResourceStream(resouceName)) 
    using(var reader = new StreamReader(stream)) 
    { 
     return reader.ReadToEnd(); 
    } 
} 
+0

답변 해 주셔서 감사합니다. 이것은 내가 실제로하고 싶은 것과 꽤 비슷하지만 위와 같이 스크립트 블록보다는 include가 필요합니다. – James

관련 문제