【问题标题】:How do you switch _layout.cshtml based on Screen vs Handheld? (.NET MVC3)您如何根据屏幕与手持设备切换 _layout.cshtml? (.NET MVC3)
【发布时间】:2011-03-09 23:56:17
【问题描述】:

我正在设计一个小型网站,希望能够在计算机和移动设备上浏览。我知道我可以使用以下方法轻松地将我的 CSS 替换为大多数移动设备:

<link rel="stylesheet" type="text/css" media="handheld" href="foo_mobile.css">
<link rel="stylesheet" type="text/css" media="screen" href="foo_screen.css">

但是,我真正想要完成的是根据浏览器类型(屏幕与手持设备 - 加上 iPhone)更换 _layout.cshtml

我看到很多网站将移动设备重定向到像 m.xyzCorp.com 这样的子域,但我希望尽可能避免这种情况。

那里有示例代码或教程吗?我的 Google-foo 今天很弱。

TIA

【问题讨论】:

  • 好问题,我很感激,@Keith Barrows

标签: asp.net-mvc-3 master-pages cross-browser


【解决方案1】:

在 _ViewStart.cshtml 中有这样的东西

@{
    if (!Request.Browser.IsMobileDevice)
    {
        Layout = "~/Views/Shared/_Layout.cshtml";
    }
    else
    {
        Layout = "~/Views/Shared/_MobileSiteLayout.cshtml";
    }
}

【讨论】:

  • 我总是对 MS 在 MVC3 中做出如此简单的事情感到惊喜!感谢您的回答,到目前为止完全符合要求。
猜你喜欢
  • 2016-06-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-30
  • 1970-01-01
  • 2020-09-04
  • 2014-01-10
相关资源
最近更新 更多