views:

205

answers:

3

Hi I am having trouble getting an embedded js file to work.

I have tried all of the following:

  • Running Cassini development server (VS2008, .NET 3.5)
  • Added [assembly: WebResource("MyNamespace.MyScriptFile.js", "text/javascript")] above the class's namespace declaration.
  • Script file has build action "Embedded Resource".
  • Tried registering during OnInit, OnLoad and OnPreRender
  • Script file is in the same assembly and namespace as the control registering it.
  • Opened assembly with Reflector and verified the name of the resource is correct.
  • Does not work using any of the following methods:

    ScriptManager.RegisterClientScriptResource(Page, GetType(), "MyNamespace.MyScriptFile.js");
    
    
    Page.ClientScript.RegisterClientScriptResource(GetType(), "MyNamespace.MyScriptFile.js");
    
    
    Page.ClientScript.RegisterClientScriptInclude(GetType(), "key",
        Page.ClientScript.GetWebResourceUrl(GetType(), "MyNamespace.MyScriptFile.js"));
    
    • Other WebResource.axd files are being found - only this one is not being found.

The request for the resource returns a 404 page with an exception listed: "*[HttpException]: This is an invalid webresource request.*"

Using the ScriptManager.RegisterClientScriptResource produces the exception:

"*Web resource 'MyNamespace.MyScriptFile.js' was not found.*"
+1  A: 

Try implementing your own ScriptManger and then adding the embedded file from there. Here's an example

public class MyScriptManager : System.Web.UI.ScriptManager
{
    protected override void OnInit(EventArgs e)
    {
            base.OnInit(e);
            RegisterClientScriptResource(this, typeof(ScriptManagerExtension), "MyNamespace.MyScriptFile.js");
    }
}
zowens
That may work but one of the purposes of using embedded resources is that you can package javascript files into assemblies that are then shipped out to different projects. The resources can then be dynamically added by the controls that need them. If I have to make my own ScriptManager then the purpose of using an embedded resource is kind of defeated.
cbp
I disagree with that assessment. Having your own script manager is a way to abstract away the need for "magic strings". If you have to write it more than once, "MyNamespace.MyScriptFile.js" would be a magic string. If I were you, I would use this approach simply because it allows for reuse and it does fulfill your purpose, no?
zowens
I tried out your solution and it is working - thanks! I used a ScriptManagerProxy instead so that I could package and distribute the embedded resource properly.
cbp
A: 

maybe your resource file is located inside folder(s) in project? if so, you should specify another name/location string in assembly and when you register the script

Page.ClientScript.RegisterClientScriptResource(GetType(), "MyNamespace.Folder1.Folder2.MyScriptFile.js");

[assembly: WebResource("MyNamespace.Folder1.Folder2.MyScriptFile.js", "text/javascript")]

usually that's a common problem

Mike
Good point, but sadly it is in the root directory.
cbp
+1  A: 

In your example code, you are making a call to GetType()... the type is used as the starting point for the search. Depending on where you are making your call to GetType(), you may not be getting back what you expect. Since ASP.NET dynamically compiles types for your pages and custom controls, GetType() may be returning a type defined in a new assembly built by ASP.NET.

You could try doing typeof(SomeType) instead, where SomeType is appropriate based on the location of your resource (ex. the control type you're working with).

Michael Petito