First step: Mobile view engine implementation
usingSYSTEM.WEB.MVC; /// <summary> ///Mobile View engine/// </summary> Public classMobileviewengine:razorviewengine { Public OverrideViewengineresult Findview (ControllerContext controllercontext,stringViewName,stringMastername,BOOLUseCache) {Viewengineresult result=NULL; varRequest =controllerContext.HttpContext.Request; if(Request. Issupportedmobiledevice () &&applicationhelper.hasmobilespecificviews) {varViewpathandname = Applicationhelper.mobileviewsdirectoryname +ViewName; Result=Base. Findview (ControllerContext, Viewpathandname, Mastername,true); if(Result = =NULL|| Result. View = =NULL) {result=Base. Findview (ControllerContext, Viewpathandname, Mastername,false); } } Else{result=Base. Findview (ControllerContext, ViewName, Mastername, UseCache); } returnresult; } }
Step two: Mobile View accessibility tools
usingSystem;usingSystem.Configuration;usingsystem.web; Public Static classApplicationhelper { Public Static BOOLHasmobilespecificviews {Get { BOOLConfigcheck; BOOL. TryParse (configurationmanager.appsettings["hasmobilespecificviews"], outConfigcheck); returnConfigcheck; } } /// <summary> ///used to enable debugging using alternative devices/// </summary> /// <param name= "Request" ></param> /// <returns></returns> Public Static BOOLIssupportedmobiledevice ( Thishttprequestbase request) { BOOLIsMobile =request. Browser.ismobiledevice; stringUserAgent =request. Useragent.tolowerinvariant (); IsMobile=IsMobile|| (Useragent.contains ("iphone") || Useragent.contains ("BlackBerry") || Useragent.contains ("Mobile") || Useragent.contains ("Windows CE") || Useragent.contains ("Opera Mini") || Useragent.contains ("Palm") || Useragent.contains ("Fennec") || Useragent.contains ("Adobeair") || Useragent.contains ("Ripple") || Useragent.contains ("ipad") || Useragent.contains ("Pad") || Useragent.contains ("iphone") || Useragent.contains ("Samsung") || Useragent.contains ("pod") ); returnIsMobile; } Public Static stringMobileviewsdirectoryname {Get { stringDirectoryName = configurationmanager.appsettings["Mobileviewsdirectoryname"]; return!string. IsNullOrEmpty (directoryname)? String.Format ("{0}/", directoryname):string. Empty; } } }
Step three: Mobile view engine access and routing configuration
Public classMvcApplication:System.Web.HttpApplication { Public Static voidregisterglobalfilters (globalfiltercollection filters) {filters. ADD (NewExceptionfilter ()); Filters. ADD (NewHandleerrorattribute ()); } Public Static voidregisterroutes (routecollection routes) {routes. Ignoreroute ("{Resource}.axd/{*pathinfo}"); //Pc-webroutes. MapRoute ("Default",//Route name "{Controller}/{action}/{id}",//URL with Parameters New{controller ="Login", action ="Index", id = urlparameter.optional}//parameter Default value ); ////mobile-webroutes. MapRoute ("Mobiledefault",//Route name "Mobile/{controller}/{action}/{id}",//URL with Parameters New{controller ="Mobilehome", action ="Index", id = urlparameter.optional}//parameter Default value ); } protected voidApplication_Start () {//Mobile Web-view engineVIEWENGINES.ENGINES.ADD (Newmobileviewengine ()); Arearegistration.registerallareas (); Globalconfiguration.configure (Webapiconfig.register); Registerglobalfilters (globalfilters.filters); RegisterRoutes (routetable.routes); } protected voidApplication_Error (Objectsender, EventArgs e) { } }
Implementation of view engine based on mobile web