views:

1611

answers:

7

Marked a javascript file as "Embedded resource"
Added WebResource attribute to my AssemblyInfo class

Now i'm trying to output the embedded javascript to my master page. All I'm getting is a "Web Resource not found" from the web resource url.


Project Assembly Name:

CompanyProduct


Project Default Namespace:

Company.Product.Web


Javascript file located:
Library/navigation.js


AssemblyInfo:

[assembly: WebResource("CompanyProduct.Library.navigation.js", "text/javascript")]


Code in master page:

Page.ClientScript.RegisterClientScriptInclude("NavigationScript", Page.ClientScript.GetWebResourceUrl(this.GetType(), "CompanyProduct.Library.navigation.js"));

Server Error in '/' Application.

The resource cannot be found.

Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable.  Please review the following URL and make sure that it is spelled correctly.

Requested URL: /WebResource.axd
Version Information: Microsoft .NET Framework Version:2.0.50727.1433; ASP.NET Version:2.0.50727.1433

+1  A: 

I think you want the full paths to be based on the namespace, not the assembly; So anywhere you have "CompanyProduct.Library.navigation.js", replace it with "Company.Product.Web.Library.navigation.js". Also, there is a method Page.ClientScript.RegisterClientScriptResource() that does what you need in one method (as opposed to using RegisterClientScriptInclude(GetWebResourceUrl()).

Chris Shaffer
Agreed with your first comment. Using Reflector I can see that my embedded javascript is `Company.Product.Web.Library.navigation.js`. Although using `RegisterClientScriptResource ` didn't seem to make a difference, still getting the error.
Naeem Sarfraz
A: 

The answer to your question completely depends on where you have this file in your actual project, and what the default namespace is. As Chris mentioned, the path you provide to the methods that register the script need the right path to locate the embedded resource. You don't just match the string you specify in your AssemblyInfo. The string has to be the correct path to the resource.

< default project namespace >/< any subfolders you have the file in >/< filename >

sliderhouserules
A: 

Turn this one;

Page.ClientScript.RegisterClientScriptInclude("NavigationScript"...

into this;

Page.ClientScript.RegisterClientScriptInclude("CompanyProduct.Library.navigation.js"...
Thomas Hansen
+1  A: 

Instead of this.GetType(), get a type from the assembly that contains the resource.. ie:

typeof(Company.Product.Web.Library.Class1)

Does that work?

meandmycode
Nope, cos there is no Class1 defined in the Library folder. It's an embedded javascript file I'm try to get at.
Naeem Sarfraz
Clearly you misunderstand the problem, the 'Class1' was just an example of a class name, you should pick whatever you want from that library, the important thing is you provide the method call a type that is from the SAME assembly as where the embed attribute is..
meandmycode
Apologies. I'm working within the same project. I can see the path using reflector but no joy getting it out as described in my question.
Naeem Sarfraz
A: 

This is clutching at straws a bit, but could it be that your asp.net isn't set up to process the webresource.axd correctly? If something has gone wrong maybe the handler tag is missing from the machine's web.config?

The http handlers tag of C:\Windows\Microsoft.NET\Framework\v2.0.50727\CONFIG\web.config should have a webresource.axd entry like this:

<httpHandlers>
    <add path="WebResource.axd" verb="GET" type="System.Web.Handlers.AssemblyResourceLoader" validate="True"/>
</httpHandlers>

Also double check there isn't any handler entry in the project's web.config that could be overriding the setting from the machine's web.config.

Helephant
A: 

Is the resource that you're adding in a different assembly to the code that you're using to generate the script tag? If that's the case, I think the this.GetType() will return a reference to a type in the wrong assembly so the web resource code won't have the right assembly to load the resource from.

I don't know for sure that this would be a problem but it seems to me that the code that generated the name would need to know what assembly the resource was on or it wouldn't be able to map back to that assembly when it received the request from the browser.

Helephant
The script is in the same directory
Naeem Sarfraz
A: 

Came to the same issue today. The problem seems to be that AssemblyResourceLoader uses the assembly containing the type provided to GetWebResourceUrl method (first parameter) which in your case is a dynamically created assembly for the master page (.master) and does not contain the resource you are looking for. I assume that your resource file is included in the same assembly as your base master page (.master.cs file) then you could use typeof to get the Type instance

Page.ClientScript.RegisterClientScriptInclude(
   "NavigationScript",
   Page.ClientScript.GetWebResourceUrl(
      typeof(MyMasterPage),
      "CompanyProduct.Library.navigation.js"));

where MyMasterPage is the name of your master page

Looks like it is also possible to use any other type declared in the same assembly where the resource is embedded.

dh