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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With