2

我们计划使用 ASP.NET-MVC 实现一个 WAP 站点。

有没有人有这方面的经验?有什么陷阱吗?

我们还将为浏览器实施一个“标准”网站。是否有可能只有一组模型和控制器,并且每个站点都有单独的视图?

4

1 回答 1

3

在大多数情况下,可能只有一组模型和控制器。做到这一点的方法是通过实现以下主题/模板引擎。[主题支持][1] 我在主题/模板引擎之上支持我的解决方案。

与文章来源的主要偏差在 Global.asax.cs 文件中,您需要在其中添加以下代码行:

protected void Application_BeginRequest(Object Sender, EventArgs e)
{
  SetTheme();
}
//this will set the responses Content Type to xhtml and is necessary as C# sends the WML response header
protected void Application_PreSendRequestHeaders(Object Sender, EventArgs e)
{
  if (this.Context.Items["themeName"].ToString() == "xhtml")
  {
    this.Context.Response.ContentType = "application/vnd.wap.xhtml+xml";
  }
}

private void SetTheme()
{
  //set the content type for the ViewEngine to utilize. 

            HttpContext context = this.Context;
            MobileCapabilities currentCapabilities = (MobileCapabilities)context.Request.Browser;
            String prefMime = currentCapabilities.PreferredRenderingMime;

            string accept = context.Request.ServerVariables["HTTP_ACCEPT"];
            context.Items.Remove("theme");
            context.Items.Remove("themeName");

            if (accept.Contains("application/vnd.wap.xhtml+xml"))
            {
                context.Items.Add("themeName", "xhtml");
            }
            else if (prefMime == "text/vnd.wap.wml")
            {
                context.Items.Add("themeName", "WAP");
            }
            if (!context.Items.Contains("themeName"))
            {
                context.Items.Add("themeName", "Default");
            }
        }

我知道我必须进行一些代码更改才能使其与 MVC 1 兼容,但我不记得确切的更改。我遇到的另一个主要问题是调试输出。为此,我使用了带有扩展名 ([User Agent Switcher][2]) 的 firefox,我已将其更改为向其添加 Accept Types。

对于 WAP2/XHTML1.2,接受类型为:text/html,application/vnd.wap.xhtml+xml,application/xhtml+xml,application/xml;q=0.9, / ;q=0.8

显然,您需要您的母版页和内容页遵守 WML 或 XHTML1.2

[1]:http : //frugalcoder.us/post/2008/11/13/ASPNet-MVC-Theming.aspx 主题支持

[2]:http ://chrispederick.com/work/user-agent-switcher/用户代理切换器

于 2009-10-06T09:18:37.127 回答