Detecting Requests from Mobile Browsers in ASP.NET [duplicate]
I have an existing web site and I would like to create a mobile version of it that is more suitable. For instance, the main site uses drop-down menus and we all know those are quite the fail on mobile devices.
I would like to redirect to my mobile version (it will be a subdomain of the current site) if I detect 开发者_Python百科a request from a mobile browser. So when they Google something and come to my site, they will automatically see the mobile version (just like Wikipedia).
Does ASP.NET provide an easy way of doing this? If not, how can I do it?
You can use the IsMobileDevice property somewhere in the Request.Browser. You need some decent browser definitions though. I use these excellent set of browser definitions: Ocean's place browser definitions.
They are really in depth and the best I've seen. I think he is currently working on .NET4 ones too.
I think the best solution is WURFL. It is more up date device description repository and it is free. The only inconvenience is .net api is GPL.
Keep it simple...
Heres the JS for the same...
Hope it helps someone..
var useragent = navigator.userAgent;
var isMobile = !!useragent.match(/iPhone|Android|Blackberry|Sony|Nokia|Motorola|Samsung/i),
isWebBrowser = !!useragent.match(/Mozilla/i);
// Redirect the call accordingly.
if(isWebBrowser && !isMobile)
//call to web portal
alert(" You seem to me... calling from Web Browser")
else if(isMobile)
//call to mobile apps
alert(" Call seems to be from Mobile device...")
else
{
// jus kiddin...
alert(" Unable to detect the device..... Please report to admin...")
}
There is a project on codeplex that you can use : Mobile Device Browser File
Project Description
The Mobile Browser Definition File contains definitions for individual mobile devices and browsers. At run time, ASP.NET uses the information in the request header to determine what type of device/browser has made the request.
This project provides a data file that when used with ASP.NET will detect the incoming mobile device and present you as the web developer with a set of 67 capabilities or properties describing the requesting device. These capabilities range from screen size to cookie support and provide all the information you need to adaptively render content for mobile phones and devices.
What is the Mobile Device Browser Definition File?
The Mobile Device Browser Definition File contains capability definitions for individual mobile devices and browsers. At run time, ASP.NET uses this .browser file, along with the information in the HTTP request header, to determine what type of device/browser has made the request and what the capabilities of that device are. This information is exposed to the developer through the Request.Browser property and allows them to tailor the presentation of their web page to suit the capabilities of the target device.
There's an article on CodeProject which provides such function.
精彩评论