Resources and class libraries

F

Frédéric Mayot

Hi,

I'm trying to embed a js file in my class library and register it in an
aspx.

I created the most simple solution I could :
+ ClassLibrary1 :
- Class1.cs
- test.js (embedded resource)
+WebSite
- Default.aspx

The page is displayed correctly with the resource url :
/WebSite2/WebResource.axd?d=xnJEzCL_Wi5ffEYd13hU2ywYJr-rFccyB13UatbX-2Q1&t=632713812869876368

When I copy/paste this link in my browser, I get a blank page. This
mechanism works for the .NET framework and not for my projet, that's weird.

I'm using VS Team Suite : 8.0.50727.42 (RTM.050727-4200)
..NET Frameword 2.0.50727

Could it be a question of authorization?

Thanks,
Fred

--------------------------------------
Content of class1.cs

[assembly: System.Web.UI.WebResource("test.js", "application/x-javascript")]
namespace ClassLibrary1
{
public class Class1 : WebControl
{
protected override void CreateChildControls()
{
base.CreateChildControls();
Literal l = new Literal();
l.Text = Page.ClientScript.GetWebResourceUrl(this.GetType(),
"test.js");
Controls.Add(l);
}
}
}
 
B

Brock Allen

Are you using a control that derives further from Class1? When you call this.GetType()
it's getting the most derived type which is not necessarily the class calling
GetType(). In this scenario, it's safer to use type(Class1). Though, I suspect
that is not your problem.

Oh, I reread your post... Try looking at the source code when you get the
blank page. I bet it's your javascript in there.
 
F

Frédéric Mayot

I tried typeof(Class1), but as expected, it didn't change anything.

The code of the blank page is a blank page, not my javascript file:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=Content-Type content="text/html;
charset=windows-1252"></HEAD>
<BODY></BODY></HTML>
 
G

Guest

I have the EXACT problem. I cannot find an answer.

All of the examples tell us to:

1. Embed your resources
Done: I know I have done it correctly because I can extract the resource at
runtime manually

2. Add a WebResourceAttribute to your assembly
Done: [assembly: WebResource("image1.jpg", "image/jpeg")]

3. Use GetWebResourceUrl from the aspx page
Done:
Page.ClientScript.GetWebResourceUrl(typeof(AssemblyWithWebResources.MyClass),
"image1.jpg");

4. Use the generated URL to retreive the image
Fails! No content.

The embeddedresource URLs NEVER return any content! I know I am doing it
correctly, because if I do it any differently (like using namespaces with the
resource name) I get all kinds of Errors, but if I do it the right way, there
are no errors: just zero content.

Somebody please help, we are desparate!

I have boiled it down as simple as possible. Feel free to have a look at:

http://fluidnature.com/test/TestOfWebResources.zip

It has 3 projects:
1. AssemblyWithWebResources
- contains an empty class and embedded resources exposed with
WebResourceAttributes

2. TestWebSite
- default page generates the web resource URLs and extracts the actual
embedded data so you can compare the results

3. AssemblyWithWebResources_UnitTests
- uses NUnitASP.dll to unit test the page results. Each URL is returned to
the client, the UnitTests try to open each URL and make sure they are not
empty. They fail.

- To test, you must first update the config file with the location of the
test page on your local webserver.
(AssemblyWithWebResources_UnitTests.dll.config or App.Config if you recompile)
 
G

Guest

I have re-posted my zip file, after removing the NUnitAsp project and
dependencies because some don't have it installed, and I fixed the solution
reference to the website directory.

Now all you need is VS2005. Extract the contents, open the solution and run
it:

http://fluidnature.com/test/TestOfWebResources.zip

Please, somebody look at this project and tell me what I did wrong embedding
these resources and expose them via GetWebResourceUrl?

Thank you very much,

coz

coz said:
I have the EXACT problem. I cannot find an answer.

All of the examples tell us to:

1. Embed your resources
Done: I know I have done it correctly because I can extract the resource at
runtime manually

2. Add a WebResourceAttribute to your assembly
Done: [assembly: WebResource("image1.jpg", "image/jpeg")]

3. Use GetWebResourceUrl from the aspx page
Done:
Page.ClientScript.GetWebResourceUrl(typeof(AssemblyWithWebResources.MyClass),
"image1.jpg");

4. Use the generated URL to retreive the image
Fails! No content.

The embeddedresource URLs NEVER return any content! I know I am doing it
correctly, because if I do it any differently (like using namespaces with the
resource name) I get all kinds of Errors, but if I do it the right way, there
are no errors: just zero content.

Somebody please help, we are desparate!

I have boiled it down as simple as possible. Feel free to have a look at:

http://fluidnature.com/test/TestOfWebResources.zip

It has 3 projects:
1. AssemblyWithWebResources
- contains an empty class and embedded resources exposed with
WebResourceAttributes

2. TestWebSite
- default page generates the web resource URLs and extracts the actual
embedded data so you can compare the results

3. AssemblyWithWebResources_UnitTests
- uses NUnitASP.dll to unit test the page results. Each URL is returned to
the client, the UnitTests try to open each URL and make sure they are not
empty. They fail.

- To test, you must first update the config file with the location of the
test page on your local webserver.
(AssemblyWithWebResources_UnitTests.dll.config or App.Config if you recompile)


Frédéric Mayot said:
Hi,

I'm trying to embed a js file in my class library and register it in an
aspx.

I created the most simple solution I could :
+ ClassLibrary1 :
- Class1.cs
- test.js (embedded resource)
+WebSite
- Default.aspx

The page is displayed correctly with the resource url :
/WebSite2/WebResource.axd?d=xnJEzCL_Wi5ffEYd13hU2ywYJr-rFccyB13UatbX-2Q1&t=632713812869876368

When I copy/paste this link in my browser, I get a blank page. This
mechanism works for the .NET framework and not for my projet, that's weird.

I'm using VS Team Suite : 8.0.50727.42 (RTM.050727-4200)
..NET Frameword 2.0.50727

Could it be a question of authorization?

Thanks,
Fred

--------------------------------------
Content of class1.cs

[assembly: System.Web.UI.WebResource("test.js", "application/x-javascript")]
namespace ClassLibrary1
{
public class Class1 : WebControl
{
protected override void CreateChildControls()
{
base.CreateChildControls();
Literal l = new Literal();
l.Text = Page.ClientScript.GetWebResourceUrl(this.GetType(),
"test.js");
Controls.Add(l);
}
}
}
 
D

Daveli

Try moving the WebResource attribute to the AssemblyInfo.cs file. That
worked for my colleague. Unfortunately it does not seem to work for me,
even if I have the exact same code (as far as I know).

/david

I have the EXACT problem. I cannot find an answer.

All of the examples tell us to:

1. Embed your resources
Done: I know I have done it correctly because I can extract the resource at
runtime manually

2. Add a WebResourceAttribute to your assembly
Done: [assembly: WebResource("image1.jpg", "image/jpeg")]

3. Use GetWebResourceUrl from the aspx page
Done:
Page.ClientScript.GetWebResourceUrl(typeof(AssemblyWithWebResources.MyClass),
"image1.jpg");

4. Use the generated URL to retreive the image
Fails! No content.
<snip>
 
D

Daveli

The default namespace for the website needs to be either empty or added
to the path. So if the default namespace is "ClassLibrary1", then
"test.js" should be changed to "ClassLibrary1.test.js" in both for the
WebResourceAttribute and in the call to GetWebResourceUrl().

If the file is in a folder (eg: ~/Foo/test.js), then the folder name
should be added too ("ClassLibrary1.Foo.test.js").

/david
 
G

Guest

I went ahead and opened an incident with MSDN. They agreed that this is a
documentation bug and did not charge my incident. My code works when I add
the Namespace to both the WebResourceAttribute and the GetWebResourceUrl call.

The documentation bug is that all of the examples (on both msdn2 and msdn
visual studio 2005) do NOT show the Namespace required anywhere, but in fact
it is required (under at least my circumstances!)

I believe their examples all exist in the same namespace (both the control
with the embedded resource AND the aspx page) so maybe in that case the
namespace isnt required...

of course, in most circumstances, custom controls cannot be expected to have
the same namespace. If this is the case, then the documentation just needs to
state clearly that cross-Namespace controls need to include the Namespace....


Here is the documentation that shows examples and no mention of Namespace
anywhere:

Online:

http://msdn2.microsoft.com/en-us/library/bzewcx2t(en-US,VS.80).aspx

Visual Studio 2005 help file:

ms-help://MS.VSCC.v80/MS.MSDN.v80/MS.NETDEVFX.v20.en/cpref13/html/T_System_Web_UI_WebResourceAttribute.htm


//examples that did NOT work when control :
[assembly: WebResource("image1.jpg", "image/jpeg")]
[assembly: WebResource("help.htm", "text/html", PerformSubstitution=true)]



Daveli said:
Try moving the WebResource attribute to the AssemblyInfo.cs file. That
worked for my colleague. Unfortunately it does not seem to work for me,
even if I have the exact same code (as far as I know).

/david

I have the EXACT problem. I cannot find an answer.

All of the examples tell us to:

1. Embed your resources
Done: I know I have done it correctly because I can extract the resource at
runtime manually

2. Add a WebResourceAttribute to your assembly
Done: [assembly: WebResource("image1.jpg", "image/jpeg")]

3. Use GetWebResourceUrl from the aspx page
Done:
Page.ClientScript.GetWebResourceUrl(typeof(AssemblyWithWebResources.MyClass),
"image1.jpg");

4. Use the generated URL to retreive the image
Fails! No content.
<snip>
 
G

Gery Dorazio

Thanks for this info to all of you. It helped after several hours of
frustrating and unfruitful diagnostics.

Fuul, it appears this also fails if the file extension does not match
exactly. Some image programs save jpeg images with JPG file rather than
lower case. When I did some testing I noticed that if the case doesn't
match between what is specified in the WebResource attribute and the
actual file name it doesn't work... You might want to let MS know about
this one also.

Thanks,
Gery
 

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top