【问题标题】:What are some best practices for detecting mobile devices in mvc3在 mvc3 中检测移动设备的一些最佳实践是什么
【发布时间】:2011-12-30 00:35:24
【问题描述】:

我正在让我的一个网站将某些页面重定向到移动版本。我已经阅读了 Steve SandersonShiju Varghese 关于属性使用和 51degrees.mobi 的博客文章。

如果设备是移动设备,我希望有一个给定的页面,dealer-locator,重定向到 /mobile/dealer-locator。为此,我使用经销商定位器控制器和必要的视图设置了一个区域。我已经直接测试了代码,它似乎可以正常工作。

当我在用户代理更改为 iPhone 3.0 的情况下从实际设备或 FF 访问该站点时,我被重定向到 /mobile。有没有一种简单的方法可以将页面路由映射到移动路由?

我应该提到该站点使用 isapi_rewrite 并且相关页面具有将 /dealer-locator 映射到 /dealerlocator 的规则,并且dealerlocator 是一个控制器。

我还注意到表单帖子似乎无法正常工作,因此暂时可以使用。

【问题讨论】:

  • 您可能想看看支持手机的 MVC 4 beta。查看更多here:

标签: asp.net-mvc-3 mobile 51degrees


【解决方案1】:

来自 Shiju Varghese 的博文(你的问题提到了),RedirectMobileDevicesToMobileAreaAttribute 类的方法:

// Override this method if you want to customize the controller/action/parameters to which
// mobile users would be redirected. This lets you redirect users to the mobile equivalent
// of whatever resource they originally requested.
protected virtual RouteValueDictionary GetRedirectionRouteValues(RequestContext requestContext)
{
    return new RouteValueDictionary(new { area = "Mobile", controller = "Home", action = "Index" });
}

可以被覆盖来处理这个问题。

在他的示例中,所有内容似乎都发送到~/Mobile/Home/Index,但您可以在任何地方发送请求。他正在创建一个新的路由表,与您在 Global.asax.cs 中为您的非移动路由定义的路由表等效。您可以替换整个路由表,也可以遍历现有路由并返回仅包含移动设备的字典。

【讨论】:

    猜你喜欢
    • 2019-01-26
    • 2013-07-14
    • 2012-02-28
    • 1970-01-01
    • 2017-04-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多