3

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

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

任何帮助将不胜感激,如果不可能,那么至少告诉我,这样我就可以停止追逐我的尾巴了。

4

2 回答 2

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

然后在我想引用我的资源的代码中,我使用以下内容。

            DynamicClientScript dcs = new DynamicClientScript(this.GetType(), "MyNamespace.MyScriptResource.js");

        dcs.Parameters.Add("myParam", "myValue");

        dcs.RegisterClientScriptInclude("scriptKey");

然后说我的脚本资源包含:

alert('$myParam$');

它将像这样输出:

alert('myValue');

我的代码也做了一些缓存(使用 DynamicScriptSessionManager),但你明白了......

干杯

于 2010-03-24T09:31:11.000 回答
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();
    }
}
于 2010-03-05T17:43:31.677 回答