gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 18:09:33 30 4
gpt4 key购买 nike

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

例如;假设我有几行 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),但你明白了......

干杯

关于c# - 在页面上注册/引用之前以编程方式修改嵌入式资源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2387893/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com