Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

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

0.00/5 (No votes)
28 Nov 2007 1  
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:

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:

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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here