.net – How to detect a mobile browser in a .NET MVC3 application

asp.net-mvc-3netrazor

I'm developing a .NET MVC3 application.

Is there a good way to detect if the user is using a mobile browser in the view (using RAZOR).
I'm wanting to differ the display logic if it's a mobile browser.

Thanks!

Best Answer

MVC3 exposes an IsMobileDevice flag in the Request.Browser object.

So in your razor code, you can query this variable and render accordingly.

For example, in your view (razor):

@if (Request.Browser.IsMobileDevice) {
  <!-- HTML here for mobile device -->
} else {
  <!-- HTML for desktop device -->
}