zoukankan      html  css  js  c++  java
  • 基于移动Web的视图引擎实现

    第一步:移动视图引擎实现
    using System.Web.Mvc;
    
        /// <summary>
        /// 移动版View引擎
        /// </summary>
        public class MobileViewEngine : RazorViewEngine
        {
            public override ViewEngineResult FindView(ControllerContext controllerContext, string viewName, string masterName, bool useCache)
            {
                ViewEngineResult result = null;
                var request = controllerContext.HttpContext.Request;
    
                if (request.IsSupportedMobileDevice() && ApplicationHelper.HasMobileSpecificViews)
                {
                    var viewPathAndName = 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);
                }
                return result;
            }
        }

     第二步:移动视图辅助工具

    using System;
    using System.Configuration;
    using System.Web;
    
    public static class ApplicationHelper
        {
            public static bool HasMobileSpecificViews
            {
                get
                {
                    bool configCheck;
                    bool.TryParse(ConfigurationManager.AppSettings["HasMobileSpecificViews"], out configCheck);
                    return configCheck;
    
                }
            }
            /// <summary>
            /// Used to enable debugging using alternative devices
            /// </summary>
            /// <param name="request"></param>
            /// <returns></returns>
            public static bool IsSupportedMobileDevice(this HttpRequestBase request)
            {
    
                bool isMobile = request.Browser.IsMobileDevice;
                string userAgent = 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")
                    );
                return isMobile;
    
            }
    
            public static string MobileViewsDirectoryName
            {
                get
                {
                    string directoryName = ConfigurationManager.AppSettings["MobileViewsDirectoryName"];
                    return !string.IsNullOrEmpty(directoryName) ? String.Format("{0}/", directoryName) : string.Empty;
                }
            }
    
        }

     第三步:移动视图引擎接入以及路由配置

     public class MvcApplication : System.Web.HttpApplication
        {
            public static void RegisterGlobalFilters(GlobalFilterCollection filters)
            {
                filters.Add(new ExceptionFilter());
                filters.Add(new HandleErrorAttribute());
            }
    
            public static void RegisterRoutes(RouteCollection routes)
            {
                routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
    
    
                //PC-Web
                routes.MapRoute(
                    "Default", // 路由名称
                    "{controller}/{action}/{id}", // 带有参数的 URL
                    new { controller = "Login", action = "Index", id = UrlParameter.Optional } // 参数默认值
                );
    
                ////Mobile-Web
                routes.MapRoute(
                    "MobileDefault", // 路由名称
                    "Mobile/{controller}/{action}/{id}", // 带有参数的 URL
                    new { controller = "MobileHome", action = "Index", id = UrlParameter.Optional } // 参数默认值
                );
            }
    
            protected void Application_Start()
            {
                
                //移动WEB-View引擎
                ViewEngines.Engines.Add(new MobileViewEngine());
                AreaRegistration.RegisterAllAreas();
                GlobalConfiguration.Configure(WebApiConfig.Register);
                RegisterGlobalFilters(GlobalFilters.Filters);
                RegisterRoutes(RouteTable.Routes);     
                            
            }
    
            
            protected void Application_Error(object sender, EventArgs e)
            {
                
            }
        }
  • 相关阅读:
    使用shell脚本备份PostgreSql
    java使用stream做二维数据统计,把List<Map>转换为Map<String,Map<String,Integer>>
    使用idea构建JavaFX项目
    SpringBoot(三) ---- SpringBoot启动加载器
    idea设置大全(三)------变量高亮设置
    延迟初始化占位类模式
    ThreadPoolExecutor构造参数分析
    postgresql插入或批量插入出现冲突时的处理方法
    java函数式接口
    MyBatis(二)批量插入insert语句拼接方式
  • 原文地址:https://www.cnblogs.com/oumi/p/7928119.html
Copyright © 2011-2022 走看看