Skip to content

Detecting Browser Overrides in MVC4

March 26, 2012

In MVC4 we now have display modes. This allows view engines to somewhat encapsulate the selection of alternative views based upon some criteria (typically some aspect of the request like the User-Agent HTTP header). This is intended to be how MVC supports different rendering for mobile devices.

The default display mode will render “.Mobile” views, partials and layout templates if the client device is a mobile browser. There is also an API to override this behavior: HttpContext.SetOverriddenBrowser. This allows you to ignore the User-Agent header for a user and pretend they are using some other device. This is typically done for mobile users that desire the “normal” desktop version of the website instead of the mobile version.

The way you check for this overridden User-Agent or HttpBrowserCapabilities is to call: HttpContext.GetOverriddenUserAgent or HttpContext.GetOverriddenBrowser. This pair of methods will return the overridden value or the normal value if there is no override. When I first saw this behavior I was a little upset because there’s no API to determine if we’re currently in override mode or not.

I suppose the only way around this is to call Request.UserAgent or Request.Browser to check the actual device’s values.

 

No comments yet

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: