WebResource is not working on the host
-
11-04-2021 - |
Вопрос
I have a class library project which contains a java script file "libscript.js" which is located in the library/Resources/Scripts
folder.
I marked "libscript.js" file as an Embedded Resource from property window.
I also added the following line of code to assemblyInfo.cs:
[assembly: WebResource("Library.Resources.Scripts.libscript.js", "text/javascript")]
Finally, I referenced the library in my website project.
When I run the project on local machine, it works fine and javascript file was loaded correctly. Also when I create an application in IIS, it works fine.
But when i upload the project to the host, the script file is not loaded and not found!
Решение
What you have done are steps 1 (embed the js as a resource) and 2 (add the webresource to assembly.cs) out of 3.
The third step is to extract the js and send it to the browser:
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
this.Page.ClientScript.RegisterClientScriptInclude(
this.GetType(), "libscript",
Page.ClientScript.GetWebResourceUrl(this.GetType(),
"Library.Resources.Scripts.libscript.js"));
}
You also need to modify the webresource definition in assembly.cs to include the PerformSubstitution parameter so that the correct path to the js is generated:
[assembly: WebResource("Library.Resources.Scripts.libscript.js", "text/javascript", PerformSubstitution = true)]