Looking into ASP.Net MVC 4.0 Mobile Development - part 2

Posted by nikolaosk on ASP.net Weblogs See other posts from ASP.net Weblogs or by nikolaosk
Published on Tue, 13 Nov 2012 18:57:00 GMT Indexed on 2012/11/13 23:01 UTC
Read the original article Hit count: 525

In this post I will be continuing my discussion on ASP.Net MVC 4.0 mobile development.

You can have a look at my first post on the subject here . Make sure you read it and understand it well before you move one reading the remaining of this post.

I will not be writing any code in this post. I will try to explain a few concepts related to the MVC 4.0 mobile functionality.

In this post I will be looking into the Browser Overriding feature in ASP.Net MVC 4.0. By that I mean that we override the user agent for a given user session.

This is very useful feature for people who visit a site through a device and they experience the mobile version of the site, but what they really want is the option to be able to switch to the desktop view.

"Why they might want to do that?", you might wonder.Well first of all the users of our ASP.Net MVC 4.0 application will appreciate that they have the option to switch views while some others will think that they will enjoy more the contents of our website with the "desktop view" since the mobile device they view our site has a quite large display. 

Obviously this is only one site. These are just different views that are rendered.To put it simply, browser overriding lets our application treat requests as if they were coming from a different browser rather than the one they are actually from.

In order to do that programmatically we must have a look at the System.Web.WebPages namespace and the classes in it. Most specifically the class BrowserHelpers.

Have a look at the picture below

 

In this class we see some extension methods for HttpContext class.These methods are called extensions-helpers methods and we use them to switch to one browser from another thus overriding the current/actual browser.

These APIs have effect on layout,views and partial views and will not affect any other ASP.Net Request.Browser related functionality.The overridden browser is stored in a cookie.

Let me explain what some of these methods do.

SetOverriddenBrowser() -  let us set the user agent string to specific value

GetOverriddenBrowser() -  let us get the overridden value

ClearOverriddenBrowser() -  let us remove any overridden user agent for the current request

 

To recap, in our ASP.Net MVC 4.0 applications when our application is viewed in our mobile devices, we can have a link like "Desktop View" for all those who desperately want to see the site with in full desktop-browser version.We then can specify a browser type override.

My controller class (snippet of code) that is responsible for handling the switching could be something like that.

public class SwitchViewController : Controller
{

 public RedirectResult SwitchView(bool mobile, string returnUrl)

{

if (Request.Browser.IsMobileDevice == mobile)

HttpContext.ClearOverriddenBrowser();

else

HttpContext.SetOverriddenBrowser(mobile ? BrowserOverride.Mobile : BrowserOverride.Desktop);

return Redirect(returnUrl);

}

}



Hope it helps!!!!

© ASP.net Weblogs or respective owner

Related posts about ASP.NET

Related posts about ASP.NET MVC 4.0