Detecting Requests from Mobile Browsers in ASP.NET

2019-02-09 22:53发布

This question already has an answer here:

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 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?

5条回答
SAY GOODBYE
2楼-- · 2019-02-09 23:19

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...")
    }
查看更多
地球回转人心会变
3楼-- · 2019-02-09 23:19

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.

查看更多
祖国的老花朵
4楼-- · 2019-02-09 23:27

There's an article on CodeProject which provides such function.

查看更多
该账号已被封号
5楼-- · 2019-02-09 23:32

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.

查看更多
6楼-- · 2019-02-09 23:38

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.

查看更多
登录 后发表回答