MIME types and AssemblyResourceLoaders

At Van Loon we where working on this scrollable panel that uses a IE behavior. If you are not familiar with these, you should check behaviors out here. To add a behavior to an HTML element, the style attribute includes a URL reference to the .htc file for the behavior that is needed:

<div style=“behavior: url(‘behaviors/mybehavior.htc’)>Whatever</div>

We concluded that the server control that rendered this HTML would only work if we included the .htc file inside of the website at a wellknown location. This dependency did not feel right,

I wrote this duplication of the AssemblyResourceLoader class from Whidbey. The AssemblyResourceBuilder class from ASP.NET Whidbey streams resources embedded inside of assemblies back from the server to the client. The URL for such a resource looks something like http://localhost/WebResource.axd?a=MyAssembly&r=image.bmp. The a and r stand for the assembly and resource name. The Whidbey implementation also includes a t parameter for a assembly time-stamp, so intelligent caching can be performed (read about in Nikil Kothari’s blog entry);

The behavior declaration then becomes something like:

<div style=“behavior: url(‘webresource.axd?a=MyAssembly&r=mybehavior.htc’)>Whatever</div>

By embedding the resource inside the assembly, the dependency is still there, but we do not need to think about including the .htc file in every website that uses it. If the file ever changes we will only need to change the assembly. This one is located in the GAC. The only thing we need to remember is to include an extra element inside the section of the web.config. And we can even place that inside of the machine.config. Then there is nothing that we need to remember.

What I found out is that the htc being streamed would not make the behavior work. After a bit of working on this problem, I needed to find out what the http headers where on the requests being made and responses given. Especially, I was interested in the MIME types that where returned, because I only guessed that it would be text/html. Using the brilliant tool ieHttpHeaders I discovered that IIS simply sets the MIME type for htc files to text/x-component. That was a new one for me. Then again, I’ve never payed much attention to the types other than image formats, application types and the usual text/html and text/xml.

Lesson learned: for .htc files you need to set the MIME type to text/x-component if you generate the .htc files dynamically and want to make your behaviors work.

This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s