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?
I noticed in MVC4, there is a built in method called Request.Browser.IsMobileDevice. How do
Share
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.cshtmlextension (for C# Razor views), MVC4 will, by convention, use this view for mobile devices.E.g. if you have this view in your
Homefolderby adding this view to your
HomefolderMVC4 will render
Index.Mobile.cshtmlon mobile devices andIndex.cshtmlon non mobile devices.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.cshtmlfor mobile devices, you could place the following in your_ViewStart.cshtmlfile