Angelo Lee's Blog
This is my kingdom .If i don't fight for it ,who will ?

If you receive the following error:  Could not download the Silverlight application. Check web server settings.

The problem might be the fact that the .xap mime type is not registered on the IIS server you are using.

If you have control over your server then you can just register the missing mime type .

If you are not that lucky you might need to find a workaround like the one below.

The idea is based on an older solution for Silverlight 1.0 and xaml files .

Since the problem is do to the fact that xap is not a registered mime type, we can cheat a little by creating an http handler that will handle requests for xap files.

The http handler will deliver the content of the xap file using a mime type that is known to the server. 

Since a xap file is actually a zip file we can use that mime type as the delivery content type.

Example:

Create a new class file called HttpXapHandler.cs.

Copy the following code to the file and add the file to your App_Code directory.

/// <summary>

/// HttpXapHandler class - handle requests to xap file through a back door nick named x-zip-compressed.

/// </summary>

public class HttpXapHandler : IHttpHandler

{

public void ProcessRequest( HttpContext context)

{

// get file name from request query string

string fileName = context.Request[ "fileName" ];

// check if the file is valid -> its up to you to validate in a way that makes sense to you...

if (!validateFile(fileName))

{

context.Response.Write(
"<br>Bad file request<br>" );

context.Response.End();

}

// set mime type to zip file because a xap file is actually a zip file

context.Response.ContentType = "application/x-zip-compressed" ;

context.Response.TransmitFile(context.Server.MapPath(fileName));

context.Response.End();

}

// naive test for valid xap file -> just test if the file requested is actualy a .xap file

public bool validateFile( string fileName)

{

fileName = fileName.ToUpper();

if ((fileName.Length > 4 ) && (fileName.Substring(fileName.Length - 4 ).CompareTo( ".XAP" ) == 0 )

)

return true ;

else

return false ;

}

public bool IsReusable

{

get

{

return false ;

}

}

}

// EOF HttpXapHandler

After you created the http handler add the following line to your web.config file inside the httpHandlers section(note the bold part):

< httpHandlers > < add verb = " * " path = " GetXapFile.ashx " type = " HttpXapHandler " validate = " false " />

</httpHandlers >

Now that we have an http xap handler our web site should be able to accept requests like this:

http://www.MySiteNameGoesHere.com/getXapFile.ashx?fileName=Silverlight2.xap

To actually use this inside a web page take a look at the next example.

Usage example:

To access the .xap files in your web pages you will need to replace each occurrence of the source=[xap file name] with source=getXapFile.ashx?fileName=[xap file name].

In your html page this will look something like the following (note the bold part):

< div id ="silverlightControlHost">

< object data ="data:application/x-silverlight," type ="application/x-silverlight-2-b2" width ="100%" height ="100%">

< param name ="source" value ="getXapFile.ashx?fileName=mySilverlight2file.xap "/>

< param name ="onerror" value ="onSilverlightError" />

< param name ="background" value ="white" />

 

< a href ="http://go.microsoft.com/fwlink/?LinkID=115261" style ="text-decoration: none;">

< img src ="http://go.microsoft.com/fwlink/?LinkId=108181" alt ="Get Microsoft Silverlight" style ="border-style: none"/>

</ a >

</ object >

< iframe style ='visibility:hidden;height:0;width:0;border:0px'></ iframe >

</ div >

good luck!

posted on 2010-11-29 09:14  Angelo Lee  阅读(172)  评论(0编辑  收藏  举报