我会先尝试 WURFL
http://wurfl.sourceforge.net/
他们有 .NET API 和非常好的代码示例。 http://wurfl.sourceforge.net/dotnet_index.php
可以帮助你的类叫做WURFLManager,它有以下方法:
使用 WURFL http://wurfl.sourceforge.net/dotnet_index.php
如果你使用 asp.net mvc,你可以使用 ActionFilter
public class MobileActionFilterAttribute : ActionFilterAttribute
{
// The WURFL database contains information about a huge number of devices and mobile browsers.
// http://wurfl.sourceforge.net/
// http://wurfl.sourceforge.net/dotnet_index.php
// http://wurfl.sourceforge.net/help_doc.php
private static readonly IWURFLManager WurflManager;
static MobileActionFilterAttribute ()
{
IWURFLConfigurer configurer = new ApplicationConfigurer();
WurflManager = WURFLManagerBuilder.Build(configurer);
}
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
HttpRequestBase request = filterContext.RequestContext.HttpContext.Request;
// We don't have ARR server for dev environment, so we still need to check to see if the current domain name is the mobile site.
if (request.Url.AbsoluteUri.StartsWith(SiteConfiguration.Current.MobileSiteAddress, StringComparison.OrdinalIgnoreCase))
{
return;
}
// Creates a WURFLRequest object from an ASP.NET HttpRequest object
WURFLRequest wurflRequest = WURFLRequestFactory.CreateRequest(HttpContext.Current.Request);
// Indicates whether the current user agent string refers to a desktop agent.
if (wurflRequest.IsDesktopRequest)
return;
// Get the information about the device
IDevice deviceInfo = WurflManager.GetDeviceForRequest(wurflRequest);
// Tells you if a device is a tablet computer (iPad and similar, regardless of OS)
bool isTablet = string.Equals(deviceInfo.GetCapability("is_tablet") ?? string.Empty, "true", StringComparison.OrdinalIgnoreCase);
if (isTablet)
{
// so we don't show the mobile site for iPad.
return;
}
// Indicates whether the current user agent string refers to a mobile device.
bool isMobileRequest = wurflRequest.IsMobileRequest;
// Tells you if a device is wireless or not. Specifically a mobile phone or a PDA are considered wireless devices, a desktop PC or a laptop are not
bool isWirelessDevice = string.Equals(deviceInfo.GetCapability("is_wireless_device") ?? string.Empty, "true", StringComparison.InvariantCultureIgnoreCase);
if (isMobileRequest && isWirelessDevice)
{
// we can redirect to the mobile site!
filterContext.Result = new RedirectResult(SiteConfiguration.Current.MobileSiteAddress);
}
}
}
还有51Degrees.Mobi
史蒂夫桑德森在他的博客上介绍了如何做到这一点
http://blog.stevensanderson.com/2010/12/17/using-51degreesmobi-foundation-for-accurate-mobile-browser-detection-on-aspnet-mvc-3/
51Degrees.Mobi Foundation 是一个开源 .NET 项目,它增强了 Request.Browser,因此它可以从 Wireless Universal Resource File (WURFL)(最全面和最新的移动设备信息数据库之一)获取信息。好消息是 51Degrees.Mobi Foundation 现在以 NuGet 包的形式提供,因此安装和更新非常容易。