基于移动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 ...
随机推荐
- centos7 安装zabbix3.4
1 打开yum安装rpm包,自动存放下载的rpm包 下次安装时,如果没有网可以自己制作yum源 打开文件 [root@localhost etc]# vim /etc/yum.conf keepcac ...
- linux修改hosts配置
参考 https://blog.csdn.net/qq_15192373/article/details/81093542 1. terminal中输入: sudo gedit /etc/hosts ...
- 【原创】基于NodeJS Express框架开发的一个VIP视频网站项目及源码分享
项目名称:视频网站项目 开发语言:HTML,CSS(前端),JavaScript,NODEJS(expres)(后台) 数据库:MySQL 开发环境:Win7,Webstorm 上线部署环境:Linu ...
- Going Home HDU - 1533(最大费用最小流)
On a grid map there are n little men and n houses. In each unit time, every little man can move one ...
- [luogu2148 SDOI2009] E&D (博弈论)
传送门 Solution 我们知道当SG不为0则先手必胜,然后就可以打表了 ̄▽ ̄ Code //By Menteur_Hxy #include <cmath> #include <c ...
- 3 Java的基本程序设计结构
本章主要内容: 一个简单的Java应用程序 注释 数据类型 变量 运算符 字符串 输入输出 控制流 大数值 数组 本章主要介绍程序设计的基本概念(如数据类型.分支以及循环)在Jav ...
- 前端开发神器之chrome 综述
作为前端工程师,也许你对chrome开发工具不陌生,但也谈不上对各个模块有深入了解. 本文主要是为chrome开发工具使用这个系列做个开篇. 参考资料: 谷歌开发者: https://develope ...
- hadoop2.3.0cdh5.0.2 升级到cdh5.7.0
后儿就放假了,上班这心真心收不住,为了能充实的度过这难熬的两天,我决定搞个大工程.....ps:我为啥这么期待放假呢,在沙发上像死人一样躺一天真的有意义嘛....... 当然版本:hadoop2.3. ...
- Maven学习总结(十一)——Maven项目对象模型pom.xml文件详解
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...
- 一个手机图表(echarts)折线图的封装
//定义一组颜色值,按顺序取出 var colorGroup = ["#6ca3c4","#76bfa3","#ea8f7a"," ...