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.*"
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).
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
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");
}
}
An alternate cause of this issue - you've been too heavy handed with your Global.asax, and you've said that everything else after your rules gives you the homepage. Not so clever of me, an hour or two wasted on that!
精彩评论