Alex Rivera | Logout

How do I detect a mobile browser in a .NET MVC3 application

Asked 2011-03-08T13:25:45.473
47

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!

Edit
Report

1 Answer

76

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 -->
}
answered 2011-03-08T13:28:04.597

Your Answer