Link to embedded resources from other resources in C #

In my web application, I include all my Java scripts as js files, which are built-in resources in the assembly, and add them to the page with ClientScriptManager.GetWebResourceUrl(). However, in some of my js files, I have links to other static assets such as image URLs. I would also like to make these assembly resources. Is there a way to fake a link to a resource? eg.

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

Maybe something like:

this.drophint = document.createElement('img');
this.drophint.src = '{resource:assembly.location.dragdrophint.gif}';
+3
source share
3 answers

I would suggest that you select web resources as a dynamic javascript associative array.

Server Side Code:

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

Then your client-side code looks like this:

this.drophint = document.createElement('img');
this.drophint.src = imgResources['drophint'];
this.anotherimg = document.createElement('img');
this.anotherimg.src = imgResources['anotherimg'];

Hope this helps.

+3
source

@Jon, , , .

, - , - (: #), ( < string, string > ) JavaScript. , , ( ).

+3

, , . javascript (, ajax json). , , stackoverflow . , - .

, . # , img- "id" , WebResourceUrl. , Jason, , List, JSON ( DataContractJsonSerializer) JSON script, javascript, buider.

? , , json, , DataContractJsonSerializer (fyi, 3.5, 2.0 3.0, aspnet ajax json serializer), , , . .

. json- MyImageResourceClass. , "img", .

, ! , , . , .

+2

Source: https://habr.com/ru/post/1696701/


All Articles