Hi!
For the release of the Blinq prototype I compiled the necessary adapter resources into a dll to keep the project files clear of unrelated adapter code so developers would be able to concentrate on the core Blinq files. The CSS Adapters were in an early stage at the time but the principle can just as well be applied to the current release.
The way to go about "pre-packaging" the adapters resources into a single dll is as follows:
1. Create a class-library project in Visual Studio. Name it "CSSAdapters". This will hold the resources and code files which will eventually be turned into the dll.
2. Add a "CSS Friendly ASP.NET Control Adapters" web site to the solution in Visual Studio.
3. Reference the class library project from the Adapters project. This will ensure the output of the class library project will be copied to the Adapter project's "bin" directory.
4. Move (don't copy) the following folders and their contents from the Adapters project to the dll project:
- App_Code/Adapters
- CSS
- JavaScript
The .cs/.vb files can be moved to the root directory of the class library project, but I recommend leaving the .css and .js files in their respective folders. The steps below assume this is the case.
5. Open the file "AssemblyInfo.cs" of the class library project. You will need add to this file in the next step.
6. Turn the CSS and JavaScript files residing in the "CSS" and "JavaScript" directories of the class library project into embedded resources:
a) Select a file in Solution Explorer.
b) In the properties window set its build action to "Embedded Resource".
c) Add a WebRessourceAttribute decoration for the resource of step a to "AssemblyInfo.cs". For JavaScript files this must look as follows:
[assembly: WebResource("CssAdapters.JavaScript.AdapterUtils.js", "application/x-javascript")]
For CSS files the format would be:
[assembly: WebResource("CssAdapters.CSS.Menu.css", "text/css")]
The first parameter of the WebResourceAttribute
expects the following syntax: ProjectNamespace.Foldername.Filename.FileExtension. Note that this is strictly case-sensitive, i.e. the casing of the resource files must match the parameter's value exactly.
d) Repeat steps a to c for each CSS and JavaScript file.
7. Now adjust the C#/VB code in the adapter classes to retrieve the resources from the assembly instead of the file system. The method you need here is
ClientScriptManager.GetWebResourceUrl , which which you could use as follows:
Page.ClientScript.GetWebResourceUrl(GetType(), "CssAdapters.CSS.Menu.css"));
Use this call to GetWebResourceUrl()
anywhere a file system path to the respective resource is required by the adapter class.
Following the above steps will package the CSS and JavaScript resources into a single dll along with the adapter classes. Please be aware that once the files are embedded in the dll you will have to recompile and upload the assembly to your web server each time you make a change to the CSS and JavaScript files.
Hint: the tricky part in making embedded resources work is to figure out the exact value for the first parameter of WebResourceAttribute
. If you get the casing wrong, retrieval of the resource will fail silently. No exception will be thrown, the file is simply not served. A helpful method to figure out the correct string for the resource is to set the build action for all the resources to "Embedded Resource" as in step 6b and then compile the dll. Then open the dll you just compiled in ildasm.exe and double-click on the "MANIFEST" section. Somewhere you will find entries that look similarly to this:
.mresource public CSSAdapters.CSS.Menu.css
{
// Offset: 0x00000E50 Length: 0x000006F7
}
The value after .mresource public
is the precise string you have to pass to the WebResourceAttribute in step 6c.
Hope this helps.
Regards,
Hardy Erlinger
Further reading on embedded resources in ASP.NET 2.0:
http://www.nikhilk.net/WebResourceAttribute.aspx
http://msdn2.microsoft.com/en-us/library/system.web.ui.webresourceattribute.aspx
NetspectrumINETA Germany