![]() |
Web Development »
Silverlight »
General
Intermediate
Hosting XAML files for Silverlight without registering MIME types on Web ServerBy Sriram ChitturiIIS 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, XAML, IIS 6, VS2005, IIS 7, Dev
|
||||||||
|
Advanced Search Add to IE Search |
|
|
|
||||||||||||||||
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.
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 !)
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.
| You must Sign In to use this message board. | |||||||||||||||
|
|||||||||||||||
|
|||||||||||||||
|
|||||||||||||||
|
|||||||||||||||
General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 28 Nov 2007 Editor: |
Copyright 2007 by Sriram Chitturi Everything else Copyright © CodeProject, 1999-2009 Web18 | Advertise on the Code Project |