Awk*_*der 24 browser silverlight caching xap
如何防止Web浏览器缓存Silverlight XAP文件?
我想这样做的原因是在开发期间我不想手动清除浏览器缓存,我正在寻找程序化方法服务器端.
在HTML页面的元素中为XAP的URL添加查询参数:
它将被忽略并打破缓存.在IE8中,有一些缓存管理工具:打开开发人员工具:
这里介绍的解决方案有点类似于迈克尔,但它是自动的,并保证客户端将始终获得新版本.根据您的具体情况,这可能效率低下.
由于Lars在他的评论中说他不在Stack Overflow上,我在这里复制响应.
<object id="Xaml1" data="data:application/x-silverlight-2,
"type="application/x-silverlight-2" width="100%" height="100%">
<%––<param name="source" value="ClientBin/SilverlightApp.xap"/>––%>
<%
string orgSourceValue = @"ClientBin/SilverlightApp.xap";
string param;
if (System.Diagnostics.Debugger.IsAttached)
{
param = "<param name=\"source\" value=\"" + orgSourceValue + "\" />";
}
else
{
string xappath = HttpContext.Current.Server.MapPath(@"") + @"\" + orgSourceValue;
DateTime xapCreationDate = System.IO.File.GetLastWriteTime(xappath);
param = "<param name=\"source\" value=\"" + orgSourceValue + "?ignore=" +
xapCreationDate.ToString() + "\" />";
}
Response.Write(param);
%>
....
</object>
Run Code Online (Sandbox Code Playgroud)
创建一个自定义http处理程序来处理*.xap文件,然后在处理程序中设置缓存选项.
像这样......
using System;
using System.IO;
using System.Web;
public class FileCacheHandler : IHttpHandler
{
public virtual void ProcessRequest(HttpContext context)
{
if (File.Exists(context.Request.PhysicalPath))
{
DateTime lastWriteTime = File.GetLastWriteTime(filePath);
DateTime? modifiedSinceHeader = GetModifiedSinceHeader(context.Request);
if (modifiedSinceHeader == null || lastWriteTime > modifiedSinceHeader)
{
context.Response.AddFileDependency(filePath);
context.Response.Cache.SetLastModifiedFromFileDependencies();
context.Response.Cache.SetCacheability(HttpCacheability.Public);
context.Response.TransmitFile(filePath);
context.Response.StatusCode = 200;
context.Response.ContentType = "application/x-silverlight-app";
context.Response.OutputStream.Flush();
}
else
{
context.Response.StatusCode = 304;
}
}
}
public DateTime? GetModifiedSinceHeader(HttpRequest request)
{
string modifiedSinceHeader = request.Headers["If-Modified-Since"];
DateTime modifiedSince;
if (string.IsNullOrEmpty(modifiedSinceHeader)
|| modifiedSinceHeader.Length == 0
|| !DateTime.TryParse(modifiedSinceHeader, out modifiedSince))
return null;
return modifiedSince;
}
}
Run Code Online (Sandbox Code Playgroud)
小智 5
我在xap文件的路径中添加了一个查询parm,以便我可以通过Versioning来管理它.
Default.aspx代码:
<param
name="source"
value="ClientBin/MySilverLightApp.xap?xapid<%=XapID %>" />
Run Code Online (Sandbox Code Playgroud)
Default.aspx.cs代码:
protected string XapID
{
get
{
Version v = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version;
if (System.Diagnostics.Debugger.IsAttached)
Response.Write(string.Format("Build: {0}.{1}.{2}.{3}", v.Major.ToString(), v.Minor.ToString(), v.Build.ToString(), v.Revision.ToString()));
return string.Format("{0}.{1}.{2}.{3}", v.Major.ToString(), v.Minor.ToString(), v.Build.ToString(), v.Revision.ToString()
}
}
Run Code Online (Sandbox Code Playgroud)