Internet tidak hanya diakses orang melalui komputer desktop, kini banyak orang akses Internet melalui piranti genggam (handphone). Oleh sebab itu, untuk membuat aplikasi Internet, perlu dipertimbangkan supaya dapat ditampilkan dan diakses dengan baik melalui browser desktop maupun piranti genggam.
Pada ASP.NET MVC kita dapat membuat tampilan dapat disesuaikan dengan browser pengguna. Untuk ASP.NET MVC 3 dapat menambahkan paket MobileViewEngines, sedangkan pada ASP.NET MVC 4 sudah merupakan fitur bawaan. Untuk menggunakan MobileViewEngines diperlukan database yang berisi daftar browser yang beredar di pasaran. Database browser yang lumayan lengkap dapat diambil dari http://51degrees.mobi/ (dapat juga diambil dari CodePlex dan NuGet).
Berikut adalah langkah penerapan MobileViewEngines pada ASP.NET MVC.
<configSections> <sectionGroup name="fiftyOne"> <section name="log" type="FiftyOne.Foundation.Mobile.Configuration.LogSection, FiftyOne.Foundation" requirePermission="false" allowDefinition="Everywhere" restartOnExternalChanges="false" allowExeDefinition="MachineToApplication"/> <section name="redirect" type="FiftyOne.Foundation.Mobile.Configuration.RedirectSection, FiftyOne.Foundation" requirePermission="false" allowDefinition="Everywhere" restartOnExternalChanges="false" allowExeDefinition="MachineToApplication"/> <section name="wurfl" type="FiftyOne.Foundation.Mobile.Detection.Wurfl.Configuration.WurflSection, FiftyOne.Foundation" requirePermission="false" allowDefinition="Everywhere" restartOnExternalChanges="false" allowExeDefinition="MachineToApplication"/> </sectionGroup> </configSections> <fiftyOne> <!--<log> element controls where and how much information should be recorded in the log. logFile The location of the log file. (Mandatory) logLevel Values include Debug|Info|Warn|Fatal and control the level of information logged. Detauls to Fatal if not specified. (Optional)--> <log logFile="~/App_Data/Log.txt" logLevel="Info"/> <!--<wurfl> element controls device data and new device handling. wurflFilePath is the path of the main wurfl file (mandatory). wurflPatches defines where any additional patch files can be located (optional). newDevicesURL provides a URL where new devices found on your server can be recorded (optional). newDeviceDetail can be "minimum" or "maximum" and controls the HTTP header information sent to location defined in newDevicesUrl (optional). useActualDeviceRoot When set to true only Wurfl devices marked with the attribute "actual_device_root" are used to provide capabilities. Child devices will continue to be used to for device matching but their capabilities will not be used. This is an advanced feature for those familiar with WURFL. (optional) capabilitiesWhiteList Using the capabilityName attribute of the add elements lists the Wurfl capabilities required by the mobile web application. Providing this list prevents the entire list of capabilities from being loaded slightly improving performance.--> <wurfl wurflFilePath="~/App_Data/wurfl.xml.gz" newDeviceDetail="maximum" newDevicesURL="http://devices.51degrees.mobi/new.ashx" useActualDeviceRoot="false"> <wurflPatches> <add name="browser_definitions" filePath="~/App_Data/web_browsers_patch.xml" enabled="true"/> </wurflPatches> </wurfl> </fiftyOne>
System.Web.Configuration.HttpCapabilitiesBase.BrowserCapabilitiesProvider = new FiftyOne.Foundation.Mobile.Detection.MobileCapabilitiesProvider();
Khusus untuk ASP.NET MVC 3, tambahkan baris berikut ViewEngines.Engines.Remove(ViewEngines.Engines.OfType<RazorViewEngine>().First()); ViewEngines.Engines.Add(new MobileCapableRazorViewEngine()); ViewEngines.Engines.Remove(ViewEngines.Engines.OfType<WebFormViewEngine>().First()); ViewEngines.Engines.Add(new MobileCapableWebFormViewEngine());
Khusus untuk ASP.NET MVC 3, tambahkan baris berikut
ViewEngines.Engines.Remove(ViewEngines.Engines.OfType<RazorViewEngine>().First()); ViewEngines.Engines.Add(new MobileCapableRazorViewEngine()); ViewEngines.Engines.Remove(ViewEngines.Engines.OfType<WebFormViewEngine>().First()); ViewEngines.Engines.Add(new MobileCapableWebFormViewEngine());
Dengan konfigurasi di atas, aplikasi mendeteksi browser yang digunakan dan dengan sendirinya merender sesuai dengan target browser.
Carsten Siemens edited Original. Comment: fixed typo