Click here to Skip to main content
15,885,537 members
Articles / Web Development / IIS

Hosting XAML Files for Silverlight without Registering MIME Types on Web Server

Rate me:
Please Sign up or sign in to vote.
4.29/5 (4 votes)
28 Nov 2007CPOL2 min read 41K   12   2
IIS rejects files with unknown type and ISP providers may not have updated their servers with the XAML MIME type. This article suggests a workaround to host XAML files.

Introduction

I am writing a simple JavaScript application using Silverlight 1.0 with no streaming and want to host the same on a public Web server. But the internet service provider is not yet WPF ready and their IIS is not responding to the unknown content type (XAML). For example, the page with Silverlight control is blank, i.e., the control is created but XAML is not loaded.

This article suggests a workaround to host simple Silverlight applications with XAML files where they are blocked by ISP or MIME type is not yet updated in IIS.

Background

After the Silverlight control is created, it contains a property called 'source' which points to the XAML file to be loaded. Refer to the code generated for Silverlight JavaScript application by Visual Studio (I am using Visual Studio 2005).

A JavaScript function called createSilverlight() is generated while referring to an XAML file on the server. When this code gets executed and Silverlight calls the server for the XAML file, if it is not returned or blocked by the web server a blank page appears (with no errors!).

Solution

One solution to get around the problem is to change the extension of XAML file to something else, like XML or TXT and change the source file name extension in createSilverLight() method to match the same. This is a quick solution if your project is deployment ready and your project deadline was yesterday!

In the long term, the above solution may become a deployment problem, as you have to change the extensions of files and also JavaScript files just before deployment (especially if your project is very dynamic and growing with more than one XAML file).

A more elegant solution which works both in development and also in production is to have a generic ASP.NET handler catering for XAML files. This does not require registering MIME types in IIS or modifying web.config files. Also note that it works only for simple Silverlight applications which depend on XAML and JavaScript and no other extensions.

The code for the generic file handler is given below:

C#
public class GetXAMLFile : IHttpHandler
{
    string fileList = ",scene.xaml,";
    public void ProcessRequest(HttpContext context)
    {
        string fname = context.Request["fname"].ToString().ToLower();
        // check if the file is in XAML file list you want to send
        // for your applications only
        if (fileList.IndexOf("," + fname + ",") == -1)
            context.Response.End();
        context.Response.ContentType = "text/xaml";
        string uri = context.Request.Url.AbsoluteUri;
        string xamlstring = System.IO.File.ReadAllText(
            context.Server.MapPath(fname));
        context.Response.Write(xamlstring);
        context.Response.Flush();
        context.Response.End();
    }
    public bool IsReusable
    {
        get
        {
            return false;
        }
    }
}

The line for checking the file requested before sending the contents is to avoid any rogue application to call this handler and get access to contents of files other than the required XAML files.

After creating the handler, change the source line in createSilverlight() function to:

C#
source: 'GetXAMLFile.ashx?fname=Scene.xaml'

Now when the Silverlight control loads, it calls the ASHX handler which will send the contents of the XAML file to the client.

Similarly to the above case with IIS and ASP.NET combination, if you are working with any other web server and facing similar problems, write and host a script of some extension type which can interpret a query string and send the contents of the XAML file. Take care to make sure that the Response content type is set to 'text/xaml' so that the client interprets it correctly.

History

  • 28th November, 2007: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralAnother way for XAP (Silverlight2) Pin
dhdiez5-Nov-08 11:36
dhdiez5-Nov-08 11:36 
GeneralAn another way Pin
lefaucheux10-Dec-07 11:45
lefaucheux10-Dec-07 11:45 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.