Mobile
Mobile
Steve asked me in a previous post for a sample application for detecting a mobile device and rendering a mobile view in ASP.NET MVC. You can download a sample MVC app here. The app is the same as the ASP.NET MVC template that the "GU" put together, with the added mobile action filter and views. I've also updated my mobile browser detection. Turns out not to be so straight forward as new phones are coming out all the time with different User Agents etc. This is the best "generic" way I could find: if (filterContext.HttpContext.Request.Browser.IsMobileDevice ||
filterContext.HttpContext.Request.ServerVariables["HTTP_ACCEPT"].Contains("text/vnd.wap.wml")...
One of the great things about MVC is the ability to direct any Controller Action to any named View that you want. This means that I'm able to detect wether the browser is a mobile device and direct them to the Mobile View: public ActionResult Index()
{
if (Convert.ToBoolean(filterContext.HttpContext.Request.Browser["IsMobileDevice"]))
{
return View("Index_mobile")
}
return View();
}
This works great. My Index_mobile.aspx view has a Master Page (just like any other view) that sets up the doc type and styles, and I can render the view as normal. The doc type I used is:
<!DOCTYPE html PUBLIC "-//WAPFORUM//DTD XHTML Mobile 1.0//EN" "http://www.wapforum.org/DTD/xhtml-mobile10.dtd">
Another way is to use...