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

Does your Silverlight application really have to end in .XAP?

0.00/5 (No votes)
21 Jan 2011 1  
We will explore if a Silverlight application must end in .XAP.

Introduction

We have heard it in books by speakers and even Microsoft: "When hosting a Silverlight application, make sure your file ends in .XAP". But does it really have to end in .XAP?

confused-man_pop016

The answer

The answer is surprisingly no. It is time to end the myth that when hosting a Silverlight application, it has to end in .XAP. Let's look at a sample Silverlight project first.

File-> New Project

When you create a new Silverlight Project and inspect the ClientBin folder, you will see that the filename ends in .XAP.

image

What do you think would happen if we renamed this file to .zip and updated our hosting page to look like the following?

<div id="silverlightControlHost">
<object data="data:application/x-silverlight-2," 
           type="application/x-silverlight-2" width="100%" height="100%">
  <param name="source" value="ClientBin/EndInXapIsCrap.zip"/>
  <param name="onError" value="onSilverlightError" />
  <param name="background" value="white" />
  <param name="minRuntimeVersion" value="4.0.50826.0" />
  <param name="autoUpgrade" value="true" />
  <a href="http://go.microsoft.com/fwlink/?LinkID=149156&v=4.0.50826.0" 
     style="text-decoration:none"><img src="http://go.microsoft.com/fwlink/?LinkId=161376" 
     alt="Get Microsoft Silverlight" style="border-style:none"/>
  </a>
</object><iframe id="_sl_historyFrame" 
  style="visibility:hidden;height:0px;width:0px;border:0px"></iframe></div>

OK, now this is interesting.

If we run the application, it works just like it does with the file named .XAP. We can view the page source on the Silverlight application and verify this.

Let's examine what happened to our actual Silverlight Project. A new .XAP file was created, but it left our .ZIP alone.

image

Let's try another extension

If it works with .zip, what about any other extension? Let's try a bogus extension like .MIKE. As you can see below, that works too.

What about the Silverlight Project properties?

So now you are probably thinking, can't we can change the filename in the Silverlight project and it will stick right? The answer is, you can, but it will change itself back after you save the project.

Default filename: EndInXapIsCrap.xap.

image

New file name - notice the .zip file: EndInXapIsCrap.zip.

image

After you save the project, it automatically appends .xap to whatever filename you entered (EndInXapIsCrap.zip.xap).

image

So, what is the point of all of this?

Well, if the server doesn't allow adding MIME types, then you can rename to whatever extension you choose. You can also setup a post-build event to run a batch file or whatever and rename the .XAP back to whatever extension we want. This is a very nifty trick that I hope someone finds useful.

Subscribe to my feed.

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