Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WebResource Hell - resource cannot be found

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
like image 229
Naeem Sarfraz Avatar asked Nov 26 '08 12:11

Naeem Sarfraz


4 Answers

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

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

Does that work?

like image 107
meandmycode Avatar answered Oct 26 '22 06:10

meandmycode


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.

like image 27
dh. Avatar answered Oct 26 '22 04:10

dh.


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()).

like image 42
Chris Shaffer Avatar answered Oct 26 '22 06:10

Chris Shaffer


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.

like image 30
Helephant Avatar answered Oct 26 '22 05:10

Helephant