Reputation: 135
Does anyone know how to identify request coming from mobile device in asp.net? I am using this code but using mini opera emulator to check whether its coming from mobile or desktop application.
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);
}
}
I am using Visual Studio and it does not have built in mobile sdk.
Thanks
Upvotes: 1
Views: 7051
Reputation: 6277
Use the WURFL resource database with a .NET api. This will give you a very extensive list of mobile capabilities. It is an open source community project which is constantly maintained with the latest devices.
51 degrees is the API that i have used in the past. The browser object is extended with various mobile specific attributes such as model, make, os and importantly if it is a mobile device. In addition the WURFL community now provides it's own WURFL .Net API. This SO question does a compare and contrast.
Interestingly tablet devices weren't coming up as mobile devices when we did our tests so you need to be aware of what is important to you when classifying these devices. If you are just concerned with smaller screen res then this is going to be enough. However if your site is interested in targeting devices that are portable then you will have to include additional logic to include then i.e. examine the model and make.
Upvotes: 0
Reputation: 5023
ASP.NET use pre-defined *.browser files on server to detect the browser capability. It's not a good approach since it always not up-to-date. You can parse the UserAgent and get the info.
Upvotes: 0