5,696,038 members and growing! (15,933 online)
Email Password   helpLost your password?
Web Development » Silverlight » General     Intermediate

Hosting XAML files for Silverlight without registering MIME types on Web Server

By Sriram Chitturi

IIS rejects files with unknown type and ISP providers may not have updated their servers with the XAML MIME type. Article suggests a work around to host XAML files.
Windows, .NET 3.0, .NET 3.5, .NET, XAML, IIS, Visual Studio, IIS 7, IIS 6, VS2005, Dev

Posted: 28 Nov 2007
Updated: 28 Nov 2007
Views: 6,905
Bookmarked: 6 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
4 votes for this Article.
Popularity: 2.45 Rating: 4.08 out of 5
1 vote, 25.0%
1
0 votes, 0.0%
2
1 vote, 25.0%
3
0 votes, 0.0%
4
2 votes, 50.0%
5
Note: This is an unedited contribution. If this article is inappropriate, needs attention or copies someone else's work without reference then please Report This Article

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 work around 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 refers to a 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 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 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 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 sends 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.

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

About the Author

Sriram Chitturi



Occupation: Web Developer
Location: United States United States

Other popular Silverlight articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 2 of 2 (Total in Forum: 2) (Refresh)FirstPrevNext
GeneralAnother way for XAP (Silverlight2)memberdhdiez12:36 5 Nov '08  
GeneralAn another waymemberlefaucheux12:45 10 Dec '07  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 28 Nov 2007
Editor:
Copyright 2007 by Sriram Chitturi
Everything else Copyright © CodeProject, 1999-2008
FileMail | Advertise on the Code Project