<p>In my web application I include all of my JavaScripts as js files that are embedded resources in the assembly, and add them to the page using <code>ClientScriptManager.GetWebResourceUrl()</code>. However, in some of my js files, I have references to other static assets like image urls. I would like to make those assembly resources as well. Is there a way to tokenize the reference to the resource? e.g.</p>

<pre><code>this.drophint = document.createElement('img');
this.drophint.src = '/_layouts/images/dragdrophint.gif';
</code></pre>

<p>Could become something like:</p>

<pre><code>this.drophint = document.createElement('img');
this.drophint.src = '{resource:assembly.location.dragdrophint.gif}';
</code></pre>

[{'answer_id': 47242, 'author': 'Jon', 'author_id': 4764, 'author_profile': 'https://Stackoverflow.com/users/4764', 'pm_score': 3, 'selected': True, 'text': '<p>I\'d suggest that you emit the web resources as a dynamic javascript associative array.</p>\n\n<p>Server side code:</p>\n\n<pre><code>StringBuilder script = new StringBuilder();\nscript.Append("var imgResources = {};");\nscript.AppendFormat("imgResources[\'{0}\'] = \'{1}\';", \n    "drophint", \n    Page.ClientScript.GetWebResourceUrl(Page.GetType(), "assembly.location.dragdrophint.gif"));\nscript.AppendFormat("imgResources[\'{0}\'] = \'{1}\';", \n    "anotherimg", \n    Page.ClientScript.GetWebResourceUrl(Page.GetType(), "assembly.location.anotherimg.gif"));\n\nPage.ClientScript.RegisterClientScriptBlock(\n    Page.GetType(),\n    "imgResources",\n    script.ToString(), \n    true);\n</code></pre>\n\n<p>Then your client side code looks like this:</p>\n\n<pre><code>this.drophint = document.createElement(\'img\');\nthis.drophint.src = imgResources[\'drophint\'];\nthis.anotherimg = document.createElement(\'img\');\nthis.anotherimg.src = imgResources[\'anotherimg\'];\n</code></pre>\n\n<p>Hope this helps.</p>\n'}, {'answer_id': 47681, 'author': 'Jason Bunting', 'author_id': 1790, 'author_profile': 'https://Stackoverflow.com/users/1790', 'pm_score': 2, 'selected': False, 'text': '<p>I don\'t particularly care for the exact implementation <a href="https://stackoverflow.com/questions/46489/referencing-embedded-resources-from-other-resources-in-c#47242"><strong>@Jon</strong></a> suggests, but the idea behind it is sound and I would concur that emitting these would be a good thing to do. </p>\n\n<p>A slightly better implementation, though this is all subjective to some degree, would be to create a server-side model (read: C# class(es)) that represents this dictionary (or simply use an instance of Dictionary&lt;string, string&gt;) and serialize that to JavaScript literal object notation. That way you are not dealing with the string hacking you see in Jon\'s example (if that bothers you).</p>\n'}, {'answer_id': 47750, 'author': 'Jon', 'author_id': 4764, 'author_profile': 'https://Stackoverflow.com/users/4764', 'pm_score': 2, 'selected': False, 'text': '<p>I concur with Jason\'s assessment of the initial solution I proposed, it can definitely be improved. My solution represents an older school javascript mentality (read, pre the emergence of ajax and JSON). There are always better ways to solve a problem, which one of the reasons why StackOverflow is so cool. Collectively we are better at the craft of programming than anyone of us on our own.</p>\n<p>Based on Jason\'s ideas I\'d revise my initial code, and revise some of what Jason suggested. Implement a <code>C#</code> class with two properties, the <code>img</code> resource <code>id</code> and a property that contains the <code>WebResourceUrl</code>.  Then, where I differ some from Jason is that rather than using a <code>Dictionary&lt;string, string&gt;</code> I\'d propose using a <code>List&lt;MyImageResourceClass&gt;</code>, which you can then in turn serialize to JSON (using <a href="http://msdn.microsoft.com/en-us/library/bb412170.aspx" rel="nofollow noreferrer">DataContractJsonSerializer</a>), and emit the JSON as the dynamic script, rather than manually generating the javascript using a string builder.</p>\n<p>Why a List? I think you may find that dictionaries when serialized to JSON, at least using the <code>DataContractJsonSerializer</code> (fyi available with the 3.5 framework only, with the 2.0 or 3.0 framework you\'d need to bolt on aspnet ajax and use is JSON serializer), are a little more cumbersome to work with than how a list would serialize. Although that is subjective.</p>\n<p>There are implications too with your client side code. Now on the client side you\'ll have an array of the JSON serialized MyImageResourceClass instances.  You\'d need to iterate through this array creating your <code>img</code> tags as you go.</p>\n<p>Hopefully, these ideas and suggestions can help get you going!  And no doubt there are other solutions.  I\'m interested to see what comes of this.</p>\n'}]
['https://Stackoverflow.com/questions/46489', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/67/']