Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect mobile device in ASP.NET Core

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.

like image 342
vishnu Avatar asked Dec 01 '22 15:12

vishnu


1 Answers

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

Device detection .NET CORE

Thank you Wangkanai

like image 125
zholinho Avatar answered Dec 04 '22 11:12

zholinho