我该如何重定向所有的查询字符串和散列在MVC4移动设备? [英] How do I redirect a mobile device with all query strings and hash in MVC4?

查看:97
本文介绍了我该如何重定向所有的查询字符串和散列在MVC4移动设备?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到MVC4,有一个内置的名为Request.Browser.IsMobileDevice方法。我如何使用它来检查,​​如果该请求是移动,然后重定向到控制器和/移动/指数的作用?我想继续在任何查询字符串,那就是在URL中的位置哈希值。是否有一个内置的方式做到这一点?

I noticed in MVC4, there is a built in method called Request.Browser.IsMobileDevice. How do I use this to check if the request is a mobile then redirect to the controller and action of /mobile/index? I want to carry over any query strings and the location hash that was in the URL. Is there a built in way to do this?

推荐答案

通常你不会想重定向到一个不同的控制器和动作的移动设备,因为移动设备应该只需要一个不同的再presentation同样的模式(你不想复制你的控制器逻辑)。如果你创建具有 .Mobile.cshtml 扩展名(C#剃刀视图)视图,MVC4会,按照惯例,使用此视图用于移动设备。

Generally you wouldn't want to redirect to a different controller and action for a mobile device since a mobile device should just require a different representation of the same model (you wouldn't want to duplicate your controller logic). If you create a view with the .Mobile.cshtml extension (for C# Razor views), MVC4 will, by convention, use this view for mobile devices.

例如。如果你在你的首页文件夹这个观点

E.g. if you have this view in your Home folder

Index.cshtml

加入这一观点,在首页文件夹

Index.Mobile.cshtml

MVC4将呈现 Index.Mobile.cshtml Index.cshtml 非移动设备在移动设备和

MVC4 will render Index.Mobile.cshtml on mobile devices and Index.cshtml on non mobile devices.

这就是说,有可能有时有必要做特定的移动设备的东西。例如。如果你想要一个不同的 _Layout.cshtml 为移动设备,你可以放在下面的 _ViewStart.cshtml 文件

That said, there may be times when it is necessary to do something specific for mobile devices. E.g. if you wanted a different _Layout.cshtml for mobile devices, you could place the following in your _ViewStart.cshtml file

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

这篇关于我该如何重定向所有的查询字符串和散列在MVC4移动设备?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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