OutputCache造成页面响应内容类型为text/vnd.wap.wml的问题

  相关文章:再次揭露text/vnd.wap.wml引起的ASP.NET OutputCache Bug及解决方法

  这段时间,访问博客园首页有时会出现text/vnd.wap.wml文件下载的对话框,如下图:

 

  出现这个问题后,等1分钟左右或者回收应用程序池就恢复正常。开始以为是IIS 7的问题,不知道从何处下手去找出原因。

  今天终于在iis.net的论坛中找到了原因:Prevent automatic content type switching to text/vnd.wap.wml of *.aspx files

  问题是在下面的情况下出现的:

  博客园首页使用了ASP.NET的页面缓存:

  <%@ OutputCache Duration="60" VaryByParam="*" VaryByCustom="FullUrl" %>

  当某个只能接受text/vnd.wap.wml内容类型的客户端在缓存过期时访问博客园首页,ASP.NET会建立响应内容类型为text/vnd.wap.wml的缓存,这时正常浏览器访问得到的就是text/vnd.wap.wml响应内容,于是出现文件下载对话框,当这个缓存过期后就恢复正常。

  下面我们用测试代码重现这个问题。

  测试页面文件为http://www.cnblogs.com/wap_test_1.aspx,代码如下:

<%@ OutputCache Duration="360" VaryByParam="none" Location="Any"%>

<html>
<head><title>Test</title></head>
<body>
<p>Hello World</p>
</body>
</html>

  通过一个控制台程序模拟只接受text/vnd.wap.wml内容类型的客户端,代码如下:

using System;
using System.IO;
using System.Net;

class Program
{
 
static void Main( string[] args )
 {
  
string url = "http://www.cnblogs.com/wap_test_1.aspx";
  HttpWebRequest request = (HttpWebRequest)WebRequest.Create( url );
  request.Method = "GET";
  request.UserAgent = "Test";
  request.Accept = "text/vnd.wap.wml";

  HttpWebResponse response = (HttpWebResponse)request.GetResponse();
  Console.WriteLine( response.Headers.ToString() );
  TextReader responseReader = new StreamReader( response.GetResponseStream() );
  
string content = responseReader.ReadToEnd();
  Console.WriteLine( content );

  Console.ReadKey();
 }
}

  测试控制台程序代码下载:wap_client_20100206.rar

  运行该程序,结果如下:

 

  然后访问这个控制台程序访问过的页面http://www.cnblogs.com/wap_test_1.aspx,就会出现文件下载对话框:

 

 

  在iis.net的帖子中提到的一种解决方法是在OutputCache中增加VaryByHeader="Content-Type",示例代码如下:

  <%@ OutputCache Duration="360" VaryByParam="none" Location="Any" VaryByHeader="Content-Type"%>

  这不是最好的解决方法,因为这需要对所有使用OutputCache的ASP.NET文件进行修改。

  我目前也没想到更好的解决方法,找到这个问题的原因后,我就立即想写随笔与大家分享,告诉大家在开发中注意这个问题,也希望大家帮忙想想,有没有更好的解决方法。

  【更新】

  更好的解决方法:

  在Global.asax中,在Application_BeginRequest方法中,添加如下的代码:

string acceptTypes = Request.Headers["Accept"];
if (!string.IsNullOrEmpty(acceptTypes) && acceptTypes.ToLower().IndexOf("text/vnd.wap.wml") >-1)
{
Response.Cache.SetCacheability(HttpCacheability.NoCache);
}
posted @ 2010-02-06 16:28  dudu  阅读(4584)  评论(32编辑  收藏  举报