domenica 4 maggio 2008

How to embed a resource in a DotNet DLL (and how to use it)

In my previous post I proposed a web custom control useful to embed videos in your web pages.
The control is made of some server-side code written in C# and some javascript functions and one flash plug-in, both client-side.
An interesting point is that all the necessary code is embedded in a single DLL, making the exploiting process in a ASP.NET panorama easier.

Step 1
In the Solution Explorer, over the file that you want to embed, right-click and choice Properties then set “”Embedded Resource” on “Build Action” :

Step 2
If the file is a web resource that you want to reference with an URL (for example a javascript file, a flash plug-in or a css file) you must open the Properties/AssemblyInfo.cs file:

Then add the following line (Filename.extension is the file to embed, Namespace is the namespace of the project or of the control and MIME_Type is the MIME type of the file):

[assembly: WebResource(“Namespace.Filename.extension”, “MIME_Type“)]

For example in my previous project:

Step 3
Using the embed file is easy, you can obtain the referencing URL with the GetWebResourceUrl method:

Page.ClientScript. GetWebResourceUrl (this.GetType(), Namespace.Filename.extension)

That’s all!

Nessun commento: