代码之家  ›  专栏  ›  技术社区  ›  James

在页面上注册/引用嵌入资源之前以编程方式修改它

  •  3
  • James  · 技术社区  · 16 年前

    首先,“修改”这个词可能是错误的,我看到一些人在网上发帖只是问他们是否真的可以修改嵌入式资源。我想做的是,在我的程序集中使用一个资源作为一种模板,在页面上注册它之前,我会对它进行查找和替换-这可能吗?

    例如;假设在我的程序集中有几行jQuery作为嵌入式资源,在这个脚本中我引用了一个CSS类名,可以由前端程序员设置。因为在实现之前我不知道CSS类是什么,所以有没有一种方法可以遍历嵌入的资源并用ThisClassName替换,$myclass$。

    2 回复  |  直到 16 年前
        1
  •  1
  •   James    16 年前

    我通过创建一个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)中的值替换每个键。

    处理程序所做的。。。

            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');
    

        2
  •  0
  •   Matt Dearing    16 年前

    在codebhind中,可以读取嵌入资源的内容,切换出所需的任何内容,然后将新内容写入响应。像这样:

    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();
        }
    }
    
    推荐文章