In this article, I will discuss migration of your existing ASP.NET MVC Framework-based application to Windows Mobile Devices, and I’ll also show you a sample MVC web application migrated to Windows Mobile. For the last 6 months I have worked on a web based application in the life science domain, and as per customer demand we introduced ASP.NET MVC Framework with a test driven approach.
Our customer was very happy about the product quality, and after project completion we got another work order to deploy the customer module to Windows Smart Devices. None of our team members had experience deploying ASP.NET applications to Windows Smart Devices. Our plan was to deploy the existing released codebase to Windows Smart Devices with minimal modification.Web Application Toolkit for Mobile Devices
We identified the Microsoft Web Application Development Toolkit for Mobile Devices as the solution. MVC Framework-based web applications can be easily extended to mobile devices using this tool. It’s also free. You can download the toolkit here.Prerequisites for the mobile application toolkit are:- 1. MS .NET Framework 3.5 with Service Pack 1 installed.
2. Visual Studio 2008 professional version or above. You can also download Microsoft Visual Web Developer 2008 Express Edition, which is free.
3. If your operating system is Windows XP, you need to install Microsoft Active Sync for synchronizing your development environment with device emulator.
4. ASP.NET MVC Framework, this framework is also free and can be downloaded from MSDN or CodePlex.
5. Windows Mobile SDK 6.0 or above. By default the SDK doesn’t have emulator images so you can download from the following path.http://www.microsoft.com/downloads/details.aspx?FamilyID=1A7A6B52-F89E-4354-84CE-5D19C204498A&displaylang=en
Once you install the Web Application Development Toolkit for Mobile, it will create a folder named WebAppToolkitMobile to the location you specified. Open the WebAppToolkitMobileVisualStudioTemplates folder and double click Mobile Web Application.vsi file to create a new Mobile Application template project in Visual Studio 2008. Now open your Visual Studio 2008 and select Mobile Web Application Project Template from ‘My Template’ Category and name you project as ‘TestMobileWebApplication’. Add a unit test project as well when the wizard asks you to do that.Now, if you look at your solution explorer, it will have three different projects. The first project named ‘MobileCapableViewEngine’ is a class library type project so the output is an assembly (.dll file) referred in your main project. In runtime, this assembly determines which view needs to be rendered depending on the browser and device type requested in that view. For example, if you want to show an employee list in your asp.net web application and you want to create the same list for the mobile application browser as well, you need to create two different views. The first one will be supported by Internet Explorer and the second one will supported by a mobile internet browser. You can also create customized views for mobile browser, as mobile and PDA screens can be small. In most of the scenarios, a developer would create customized views for mobile applications. The ViewEngineResult function of MobileCapableViewEngine class determines at runtime which view need to be rendered.
public override ViewEngineResult FindView(ControllerContext controllerContext,
string viewName, string masterName, bool useCache)        {            ViewEngineResult result = null;            HttpRequestBase request = controllerContext.HttpContext.Request;            if (request.Browser.IsMobileDevice)            {                string mobileViewName = string.Empty;                mobileViewName = string.Format(                                        CultureInfo.InvariantCulture,                                        "Mobile/{0}/{1}",                                        this.RetrieveDeviceFolderName(request.Browser.Browser),                                        viewName);                result = this.ResolveView(controllerContext, mobileViewName, masterName, useCache);                if (result == null || result.View == null)                {                    mobileViewName = string.Format(                                            CultureInfo.InvariantCulture,                                            "Mobile/{0}",                                            viewName);                    result = this.ResolveView(controllerContext, mobileViewName, masterName, useCache);                }            }                       if (result == null || result.View == null)            {                result = this.ResolveView(controllerContext, viewName, masterName, useCache);            }            return result;        }        protected virtual ViewEngineResult ResolveView(ControllerContext controllerContext, string        viewName, string masterName, bool useCache)        {            return base.FindView(controllerContext, viewName, masterName, useCache);        }
The next thing you need to do is register all you device and browser type in Application_Start event exists in your Global.asax file. The MobileCapableViewEngine assembly configures 2 default device folder mappings; one for iPhone that being mapped with ViewHomeMobileiPhone folder and the other one for Windows Mobile maps to ViewHomeMobileWindowsMobile folder. Developers can remove or add new folder mappings using the DeviceFolders property from the MobileCapableWebFormViewEngine class before adding the instance to the engines collections in the RegisterViewEngines function. See the following code snipped.
public static void RegisterViewEngines(ViewEngineCollection engines) Â Â Â Â Â Â Â { Â Â Â Â Â Â Â Â Â Â Â var engine = new MobileCapableWebFormViewEngine(); Â Â Â Â Â Â Â Â Â Â Â engine.DeviceFolders.Clear(); Â Â Â Â Â Â Â Â Â Â Â engine.DeviceFolders.Add("Pocket IE", "WindowsMobile"); Â Â Â Â Â Â Â Â Â Â Â engine.DeviceFolders.Add("AppleMAC-Safari", "iPhone"); Â Â Â Â Â Â Â Â Â Â Â engine.DeviceFolders.Add("Safari", "iPhone"); Â Â Â Â Â Â Â Â Â Â Â engines.Clear(); Â Â Â Â Â Â Â Â Â Â Â engines.Add(engine); Â Â Â Â Â Â Â }