Implementation of view engine based on mobile web

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.