asp.net mvc 不断用 .wml 覆盖 text/html 内容类型 [英] asp.net mvc keeps overriding text/html content-type with .wml

查看:17
本文介绍了asp.net mvc 不断用 .wml 覆盖 text/html 内容类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm developing an website which is to be viewed on mobile (cellphone) devices. I'm just using plain HTML 4.01, nothing special at all. The pages render fine on all the mobile browsers we've tested, except for Nokia Series 40 1-5th editions. On closer inspection, it seems that IIS is automatically rendering the html with the content-type of text/vnd.wap.wml instead of text/html. Since we're not using WAP, the page fails with an error.

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

I'm using ASP.Net MVC 1.0 so I've added a ActionFilterAttribute to override the content-type. This code runs but still comes out as vnd.wap.wml on the client side.
I'm using this doctype;

<?xml 版本="1.0" 编码="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 修复而不是代码修复.

It's worth noting that the vnd.wap.wml is the first Accept-Encoding specified by the mobile browser, so I assume IIS7 is serving it up for that reason. And I guess as MVC doesn't specifically refer to .html (or .aspx) files, maybe the mime-type is being skipped? I suspect this is probably an IIS fix rather than a code-fix.

非常感谢任何帮助!

推荐答案

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

Turns out I hadn't implemented the ActionFilter correctly.. I needed to override the OnResultExecuted method in addition to the OnActionExecuted method. The full attribute looks like this (just add [HtmlOverrideFilter] to your Controllers where needed). Hope this helps someone.

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";
    }
}

这篇关于asp.net mvc 不断用 .wml 覆盖 text/html 内容类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆