问题
If a web application, how can you figure out the visitor is viewing from a mobile phone?
is it also possible figure out the make/model like blackberry versus iphone?
回答1:
You could check their User-Agent string.
回答2:
Scott Hanselman did a podcast on a module device database that Microsoft made available on Codeplex. It's a .browser file that you put in your ASP.NET (version 2.0+) website, which then helps the framework define the Request.Browser property more closely. You should be able to get a lot of the information that you need from there.
However, that project is no longer supported, and when you're dealing with user agent detection, having an up-to-date resource is very important. You should probably use a similar project, such as 51degrees.mobi or WURFL.
Even without that, at the least you can check Request.Browser.IsMobileDevice.
回答3:
You can check the User-Agent string. In JavaScript, that's really easy, it's just a property of the navigator object.
var useragent = navigator.userAgent;
You can check if the device if iPhone or Blackberry in JS with something like
var isIphone = !!agent.match(/iPhone/i),
isBlackberry = !!agent.match(/blackberry/i);
if isIphone is true you are accessing the site from an Iphone, if isBlackBerry you are accessing the site from a Blackberry.
You can use "UserAgent Switcher" plugin for firefox to test that.
回答4:
Use httpRequest.Browser.IsMobileDevice in Session_Start
void Session_Start(object sender, EventArgs e)
{
// Redirect mobile users to the mobile home page
HttpRequest httpRequest = HttpContext.Current.Request;
if (httpRequest.Browser.IsMobileDevice)
{
string path = httpRequest.Url.PathAndQuery;
bool isOnMobilePage = path.StartsWith("/Mobile/",
StringComparison.OrdinalIgnoreCase);
if (!isOnMobilePage)
{
string redirectTo = "~/Mobile/";
HttpContext.Current.Response.Redirect(redirectTo);
}
}
}
回答5:
You need to check for User Agent.
Like... http://www.developershome.com/wap/detection/detection.asp?page=userAgentHeader
回答6:
Here is some information from a similar question:
Auto detect mobile browser (via user-agent?)
It involves reading the user-agent header. The answers to that other question include links for scripts for this.
Here is another helpful discussion:
Standard way to detect mobile browsers in a web application based on the http request
回答7:
As preciously stated - user agent
BUT, do you really mean to ask "is it a mobile phone"? Or do you really mean something else?
Lines are blurring these days. I bought a nice little Android slate with 7" screen from eBay for $99. Is that a mobile phone? Is a mini-netbook with 6" screen? Is a Kidnle-like-device?
I'm just wondering why you want to know if it is a mobile 'phone ... screen size? processing power? something else?
You probably did mean mobile 'phone but, if not, please rephrase and we can help further.
回答8:
Please, check this out. Worked fine for me!
http://detectmobilebrowsers.com/
来源:https://stackoverflow.com/questions/2022897/how-to-detect-a-mobile-phone-in-a-web-application