Use XAP Handler with Silverlight

By Peter Bromberg

You can use IHttpHandler instead of a direct link to your .xap file. This means the url to your .xap file should be changed from "webapp/slapp.xap" to "webapp/handler.axd?source=slapp.xap". Then you will be able to add an additional parameter to the url, for example "build number": "webapp/handler.axd?source=slapp.xap&buildno;=".

public class XapHandler : IHttpHandler
{
#region IHttpHandler Members
public bool IsReusable
{
get { return true; }
}

public void ProcessRequest(HttpContext context)
{
string UrlPath = context.Request.Params["source"];
if (string.IsNullOrEmpty(UrlPath))
{
context.Response.StatusCode = ;
return;
}
string PhysicalFileName = context.Request.MapPath(UrlPath);
if (!System.IO.File.Exists(PhysicalFileName))
{
context.Response.StatusCode = ;
return;
}
context.Response.StatusCode = ;
context.Response.ContentType = "application/x-silverlight-";
try
{
System.IO.FileStream Stream = new System.IO.FileStream(PhysicalFileName, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.None);
try
{
byte[] Buffer = new byte[];
int BytesRead = ;
do
{
BytesRead = Stream.Read(Buffer, , Buffer.Length);
context.Response.OutputStream.Write(Buffer, , BytesRead);
} while (BytesRead == Buffer.Length);
}
finally
{
Stream.Close();
}
}
catch
{
context.Response.StatusCode = ;
return;
}
context.Response.OutputStream.Flush();
}

#endregion
}

Use XAP Handler with Silverlight  (1965 Views)