基于移动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 ...
随机推荐
- 【转载】文件上传Expected MultipartHttpServletRequest: is a MultipartResolver错误解决
引入包commons-fileupload-*.jar,版本号可以根据项目情况调整: 在spring mvc配置文件中增加配置,文件大小限制可根据项目情况调整: <!-- 上传文件拦截,设置最大 ...
- 内核调试-perf introduction
perf概念 perf_event Perf_events是目前在Linux上使用广泛的profiling/tracing工具,除了本身是内核(kernel)的组成部分以外,还提供了用户空间(user ...
- 【原创】使用JS封装的一个小型游戏引擎及源码分享
1 /** * @description: 引擎的设计与实现 * @user: xiugang * @time: 2018/10/01 */ /* * V1.0: 引擎实现的基本模块思路 * 1.创建 ...
- enote笔记语言(4)(ver0.4)——“5w1h2k”分析法
章节:“5w1h2k”分析法 what:我想知道某个“关键词(keyword)”(即,词汇.词语,或称单词,可以是概念|专业术语|.......)的定义. why:我想分析and搞清楚弄明白“事物 ...
- MySQL中是索引
MySQL中是索引: --.唯一索引: 一行中的内容不能一样, create t2( id int , num int, unique weiyisuiyin (id,num) ) --唯一; --约 ...
- Python中的@property装饰器
要了解@property的用途,首先要了解如何创建一个属性. 一般而言,属性都通过__init__方法创建,比如: class Student(object): def __init__(self,n ...
- 第五节:numpy之数组维度处理
- BZOJ 1572 USACO 2009 Open 工作安排
先把工作按照Deadline从小到大排序 然后按顺序取,deadline大于现在总用时就取,等于现在总用时就从前面已取的工作中找一个P最小的同它比较,取P较大的一个 用优先队列维护已取工作中P的最小值 ...
- (问题待解决)sql查询不显示前置‘0’的问题
SELECT * , ,),() ) ' end)) from CourseBooksNotes ),)),)+'秒'; ),)),)),)+'秒'
- GeoTrust 企业(OV)型 SSL证书
GeoTrust True BusinessID SSL证书属于企业验证(OV)级别的SSL证书,验证域名所有权,验证企业单位信息,提供40位/56位/128位,最高支持256位自适应加密,被20 ...