Detect mobile device in ASP.NET Core
Solution 1
You can use the manual method outlined here: https://stackoverflow.com/a/13086894/1419970
Or you can use this library: http://www.nuget.org/packages/51Degrees.mobi/3.2.10.3-beta
Both will do for you.
Solution 2
I found great library. It's very easy to use. I am not sure is it 100% reliable, but it's covering all my cases.
Example:
public class HomeController : Controller
{
private readonly IDevice device;
public HomeController(IDeviceResolver deviceResolver)
{
this.device = deviceResolver.Device
}
public IActionResult Index()
{
if(device.Type == DeviceType.Desktop)
{
//some logic
}
else if(device.Type == DeviceType.Mobile)
{
//some logic
}
else if(device.Type == DeviceType.Tablet)
{
//some logic
}
}
}
Thank you Wangkanai
Solution 3
Simple solution
public static class Extentions
{
public static bool IsMobile(string userAgent)
{
if (string.IsNullOrEmpty(userAgent))
return false;
//tablet
if (Regex.IsMatch(userAgent, "(tablet|ipad|playbook|silk)|(android(?!.*mobile))", RegexOptions.IgnoreCase))
return true;
//mobile
const string mobileRegex =
"blackberry|iphone|mobile|windows ce|opera mini|htc|sony|palm|symbianos|ipad|ipod|blackberry|bada|kindle|symbian|sonyericsson|android|samsung|nokia|wap|motor";
if (Regex.IsMatch(userAgent, mobileRegex, RegexOptions.IgnoreCase)) return true;
//not mobile
return false;
}
}
use:
var isMobile = Extentions.IsMobile(Context.Request.Headers["user-agent"].ToString());
Solution 4
Or you can use this free library DeviceDetector.NET.
This is a port of the popular PHP device-detector library to C#.
Here is how to use it.
DeviceDetectorNET.DeviceDetector.SetVersionTruncation(VersionTruncation.VERSION_TRUNCATION_NONE);
var userAgent = Request.Headers["User-Agent"];
var result = DeviceDetectorNET.DeviceDetector.GetInfoFromUserAgent(userAgent);
var output = result.Success ? result.ToString().Replace(Environment.NewLine, "<br />") : "Unknown";
vishnu
Updated on February 10, 2022Comments
-
vishnu over 2 years
I have an application which uses mobile views and desktop views as different html pages. Now I am moving that to Asp.Net core. I am not thinking about Bootstrap due to some technical reasons. I have to detect the request is from Mobile or not in StartUp to load respective Layout page. How can I achieve that ? Looking for something similar to IsMobileDevice. Already tried MvcDeviceDetector 0.1.0-t00349acaa. It didn't worked as I am using .net version 4.6.1.
-
vishnu over 7 yearsUnable to resolve '51Degrees.mobi (>= 3.2.9.1)' for '.NETFramework,Version=v4.6.1'.
-
vishnu over 7 years**Unable to resolve '51Degrees.mobi (>= 3.2.9.1)' for '.NETFramework,Version=v4.6.1'. ** This is the error I am getting when I tried 51degrees. It is not supporting .net version 4.6.1 I guess.
-
vishnu over 7 yearsI guess it will give me a solution. Just I need to change the method to compact with .net core.
-
Sweetie over 4 yearsCan we check device globally and not in each controller or action? Actually I wanted to render mobile views for mobile device and desktop views for desktop device
-
zholinho over 4 yearsI think it can be done in middleware on the same way.
-
Sylwia M about 2 yearsBest solution I've found. No need to install any packages and troubleshoot class relation and proper usage. thx!