首先,“修改”可能是错误的用语,我看到有几个人在网上发帖只是问他们是否实际上可以修改嵌入的资源。什么,我想给,是使用资源在我的组装作为一种模板,我会做一个查找和登录到页面上之前更换的 - 这是可能的。

例如;说我有jQuery的几行作为嵌入资源在我的组装和在这个剧本,我引用可由前端程序员设置一个CSS类名。因为我不知道CSS类将是什么,直到实现,有没有通过嵌入的资源去和更换,说,$ MyClass的$用ThisClassName的方式。

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

有帮助吗?

解决方案

我已经通过创建一个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),但你的想法......

干杯

其他提示

在你的代码隐藏,你可以读取嵌入资源的内容,切换出任何你想要的,然后再写入新内容的响应。是这样的:

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();
    }
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top