我们计划使用ASP.NET-MVC实现一个WAP站点。
有没有人有这方面的经验?有没有Gotchas?
我们还将为浏览器实现一个“标准”网站。有没有可能只有一组模型和控制器,并且每个站点都有单独的视图?
发布于 2009-10-06 09:18:37
在大多数情况下,可能只有一组模型和控制器。要做到这一点,可以通过实现以下主题/模板引擎。主题支持我将我的解决方案放在主题/模板引擎之上。
与文章源代码的主要差异是在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兼容,但我记不清具体的修改了。我遇到的另一个主要问题是调试输出。为此,我使用了firefox的一个扩展(User Agent Switcher),我已经对其进行了更改,为其添加了接受类型。
对于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/用户代理切换器
https://stackoverflow.com/questions/1524280
复制相似问题