【问题标题】:asp.net mvc keeps overriding text/html content-type with .wmlasp.net mvc 不断用 .wml 覆盖 text/html 内容类型
【发布时间】:2009-07-24 10:55:50
【问题描述】:

我正在开发一个可以在移动(手机)设备上查看的网站。我只是使用普通的 HTML 4.01,没什么特别的。这些页面在我们测试过的所有移动浏览器上都可以正常显示,诺基亚 Series 40 1-5 版除外。仔细观察,似乎 IIS 会自动呈现内容类型为 text/vnd.wap.wml 而不是 text/html 的 html。由于我们没有使用 WAP,因此页面失败并出现错误。

我使用的是 ASP.Net MVC 1.0,所以我添加了一个 ActionFilterAttribute 来覆盖内容类型。此代码运行,但在客户端仍以 vnd.wap.wml 的形式出现。
我正在使用这个文档类型;

<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

值得注意的是,vnd.wap.wml 是移动浏览器指定的第一个Accept-Encoding,因此我假设 IIS7 正在为此提供服务。而且我猜由于 MVC 没有专门引用 .html (或 .aspx)文件,也许 mime 类型被跳过了?我怀疑这可能是 IIS 修复而不是代码修复。

非常感谢任何帮助!

【问题讨论】:

    标签: asp.net-mvc iis-7 wml


    【解决方案1】:

    原来我没有正确实现 ActionFilter。除了 OnActionExecuted 方法之外,我还需要重写 OnResultExecuted 方法。完整的属性看起来像这样(只需将 [HtmlOverrideFilter] 添加到需要的控制器中)。希望这可以帮助某人。

    internal class HtmlOverrideFilter : ActionFilterAttribute
    {
        public override void OnActionExecuted(ActionExecutedContext filterContext)
        {
            filterContext.HttpContext.Response.ContentType = "text/html";
        } 
    
        public override void OnResultExecuted(ResultExecutedContext filterContext)
        {
            filterContext.HttpContext.Response.ContentType = "text/html";
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2010-11-01
      • 2023-03-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多