在我的 Web 应用程序中,我将所有 JavaScript 作为 js 文件包含在程序集中,并使用 ClientScriptManager.GetWebResourceUrl()
将它们添加到页面。但是,在我的一些 js 文件中,我引用了其他静态 Assets ,例如图像 url。我也想制作那些装配资源。有没有办法标记对资源的引用?例如
this.drophint = document.createElement('img');
this.drophint.src = '/_layouts/images/dragdrophint.gif';
可以变成这样:
this.drophint = document.createElement('img');
this.drophint.src = '{resource:assembly.location.dragdrophint.gif}';
最佳答案
我建议您将 Web 资源作为动态 javascript 关联数组发出。
服务器端代码:
StringBuilder script = new StringBuilder();
script.Append("var imgResources = {};");
script.AppendFormat("imgResources['{0}'] = '{1}';",
"drophint",
Page.ClientScript.GetWebResourceUrl(Page.GetType(), "assembly.location.dragdrophint.gif"));
script.AppendFormat("imgResources['{0}'] = '{1}';",
"anotherimg",
Page.ClientScript.GetWebResourceUrl(Page.GetType(), "assembly.location.anotherimg.gif"));
Page.ClientScript.RegisterClientScriptBlock(
Page.GetType(),
"imgResources",
script.ToString(),
true);
然后您的客户端代码如下所示:
this.drophint = document.createElement('img');
this.drophint.src = imgResources['drophint'];
this.anotherimg = document.createElement('img');
this.anotherimg.src = imgResources['anotherimg'];
希望这对您有所帮助。
关于c# - 从 C# 中的其他资源引用嵌入式资源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46489/