• 基于移动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)
            {
                
            }
        }
  • 相关阅读:
    类的组合
    类的继承和派生
    面向对象编程
    正则表达式
    sys模块 logging模块 序列化模块
    time 模块,random模块,os模块
    递归函数
    interface有没有继承Object
    UTF-8和GBK的区别
    九皇后
  • 原文地址:https://www.cnblogs.com/oumi/p/7928119.html
Copyright © 2020-2023  润新知