基于移动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)
{ }
}
基于移动Web的视图引擎实现的更多相关文章
- (翻译)为你的MVC应用程序创建自定义视图引擎
Creating your own MVC View Engine For MVC Application 原文链接:http://www.codeproject.com/Articles/29429 ...
- 教你50招提升ASP.NET性能(二):移除不用的视图引擎
(2)Remove unused View Engines 招数2: 移除不用的视图引擎 If you're an ASP.NET MVC developer, you might not know ...
- ASP.NET MVC 5 Web编程4 -- Razor视图引擎
Razor简介 Razor是ASP.NET新增的一个视图引擎,由微软全球最年轻的副总裁,有着"ASP.NET之父"称呼的Scott Guthrie主导的团队开发. 主导Razor开 ...
- 【Node.js】一、搭建基于Express框架运行环境+更换HTML视图引擎
1)安装express generator生成器 这个express generator生成器类似于vue-cli脚手架工具,用来创建一个后端项目,首先我们要对生成器进行全局安装,在cmd中输入下 ...
- 移除apsx视图引擎,及View目录下的web.config的作用
<> 使用Rezor视图引擎的时候移除apsx视图引擎 Global.asax文件 using System; using System.Collections.Generic; usin ...
- ASP.NET WEB应用程序(.network4.5)MVC Razor视图引擎2 HtmlHelper-超链接方法
一.@Html.ActionLink()概述 在MVC的Rasor视图引擎中,微软采用一种全新的方式来表示从前的超链接方式,它代替了从前的繁杂的超链接标签,让代码看起来更加简洁.通过浏览器依然会解析成 ...
- ASP.NET WEB应用程序(.network4.5)MVC Razor视图引擎2
https://www.bbsmax.com/A/gAJG67OXzZ/ 在MVC3.0版本的时候,微软终于引入了第二种模板引擎:Razor.在这之前,我们一直在使用WebForm时代沿留下来的ASP ...
- Nancy 学习-视图引擎 继续跨平台
前面一篇,讲解Nancy的基础,以及Nancy自宿主,现在开始学习视图引擎. Nancy 目前支持两种 一个是SSVE 一个是Razor.下面我们一起学习. The Super Simple View ...
- ASP.NET MVC Razor视图引擎攻略
--引子 看下面一段MVC 2.0的代码. <%if (Model != null){%> <p><%=Model%></p><%}%>&l ...
随机推荐
- seam的定时轮巡
青岛的项目要做一个功能,每天凌晨2点的时候保存一次设备数据,这个就要求项目能够间隔24小时每天去做这个事,是一个自主轮巡. seam框架正好提供了这个功能,@Expiration指定开始时间,@Int ...
- Python星号表达式
有时候可能想分解出某些值然后丢弃它们,可以使用诸如 _ 或者 ign(ignored)等常用来表示待丢弃值的变量名: record = ('ACME', 50, 123.45, (12, 18, 20 ...
- golang bytes 包
类型 []byte 的切片十分常见,Go 语言有一个 bytes 包专门用来解决这种类型的操作方法. bytes 包和字符串包十分类似.而且它还包含一个十分有用的类型 Buffer: import & ...
- common_functions.h:64:24: error: token ""__CUDACC_VER__ is no longer supported.
问题在复现工程https://github.com/google/hdrnet时出现. 现象: 解决: TensorFlow版本问题,升级到版本1.10.0之后,问题解决.
- SGU100
Read integers A and B from input file and write their sum in output file. Input Input file contains ...
- rsync全网备份
rsync备份企业方案 企业有Linux服务器又有windows服务器,备份用rsync(服务端),Linux(客户端),Windows(客户端,cwrsync,旧版本有免费版)打包压缩数据往服务器上 ...
- 32.修改IK分词器源码来基于mysql热更新词库
主要知识点, 修改IK分词器源码来基于mysql热更新词库 一.IK增加新词的原因 在第32小节中学习到了直接在es的词库中增加词语,来扩充自已的词库,但是这样做有以下缺点: (1)每次添加完 ...
- Python类中的 私有变量和私有方法
默认情况下,Python中的成员函数和成员变量都是公开的(public),在python中没有类似public,private等关键词来修饰成员函数和成员变量.在python中定义私有变量只需要在变量 ...
- LightOJ 1370 Bi-shoe and Phi-shoe
/* LightOJ 1370 Bi-shoe and Phi-shoe http://lightoj.com/login_main.php?url=volume_showproblem.php?pr ...
- 造成segment fault,产生core dump的可能原因
1.内存访问越界 a) 由于使用错误的下标,导致数组访问越界 b) 搜索字符串时,依靠字符串结束符来判断字符串是否结束,但是字符串没有正常的使用结束符 c) 使用strcpy, strcat, spr ...