第一步:移动视图引擎实现
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的视图引擎实现的更多相关文章

  1. (翻译)为你的MVC应用程序创建自定义视图引擎

    Creating your own MVC View Engine For MVC Application 原文链接:http://www.codeproject.com/Articles/29429 ...

  2. 教你50招提升ASP.NET性能(二):移除不用的视图引擎

    (2)Remove unused View Engines 招数2: 移除不用的视图引擎 If you're an ASP.NET MVC developer, you might not know ...

  3. ASP.NET MVC 5 Web编程4 -- Razor视图引擎

    Razor简介 Razor是ASP.NET新增的一个视图引擎,由微软全球最年轻的副总裁,有着"ASP.NET之父"称呼的Scott Guthrie主导的团队开发. 主导Razor开 ...

  4. 【Node.js】一、搭建基于Express框架运行环境+更换HTML视图引擎

      1)安装express generator生成器 这个express generator生成器类似于vue-cli脚手架工具,用来创建一个后端项目,首先我们要对生成器进行全局安装,在cmd中输入下 ...

  5. 移除apsx视图引擎,及View目录下的web.config的作用

    <> 使用Rezor视图引擎的时候移除apsx视图引擎 Global.asax文件 using System; using System.Collections.Generic; usin ...

  6. ASP.NET WEB应用程序(.network4.5)MVC Razor视图引擎2 HtmlHelper-超链接方法

    一.@Html.ActionLink()概述 在MVC的Rasor视图引擎中,微软采用一种全新的方式来表示从前的超链接方式,它代替了从前的繁杂的超链接标签,让代码看起来更加简洁.通过浏览器依然会解析成 ...

  7. ASP.NET WEB应用程序(.network4.5)MVC Razor视图引擎2

    https://www.bbsmax.com/A/gAJG67OXzZ/ 在MVC3.0版本的时候,微软终于引入了第二种模板引擎:Razor.在这之前,我们一直在使用WebForm时代沿留下来的ASP ...

  8. Nancy 学习-视图引擎 继续跨平台

    前面一篇,讲解Nancy的基础,以及Nancy自宿主,现在开始学习视图引擎. Nancy 目前支持两种 一个是SSVE 一个是Razor.下面我们一起学习. The Super Simple View ...

  9. ASP.NET MVC Razor视图引擎攻略

    --引子 看下面一段MVC 2.0的代码. <%if (Model != null){%> <p><%=Model%></p><%}%>&l ...

随机推荐

  1. CSS 之自定义滚动条样式

    ::-webkit-scrollbar {/*滚动条整体样式*/ width: 5px; /*高宽分别对应横竖滚动条的尺寸*/ height: 1px; } ::-webkit-scrollbar-t ...

  2. CAD从二制流数据中加载图形(com接口VB语言)

    主要用到函数说明: _DMxDrawX::ReadBinStream 从二制流数据中加载图形,详细说明如下: 参数 说明 VARIANT varBinArray 二制流数据,是个byte数组 BSTR ...

  3. Luogu P2970 [USACO09DEC]自私的放牧

    https://www.luogu.org/problemnew/show/P2970 P2970 [USACO09DEC]自私的放牧 题目描述 Each of Farmer John's N (1 ...

  4. 举枪消灭"烂代码"的实战案例

    前言 之前我写过一篇如何少写PHP "烂"代码 https://segmentfault.com/a/11...感觉很多新人对此不太理解.今天以打卡功能为例,去讲解其中的奥秘.那篇 ...

  5. ansible - playbook(剧组)

    目录 ansible - playbook(剧组) 常用命令 五种传参方式 常用元素详解 tags handlers template when 循环 嵌套循环 ansible - playbook( ...

  6. SqlServer转换为Mysql(mss2sql)

    SqlServer转换为Mysql(mss2sql)工具 http://pan.baidu.com/s/1c2d8R8O 参考链接: http://www.cnblogs.com/angestudy/ ...

  7. C#关键字详解第二节

    base:基类 在有些书中base的解释为表示父类,没错,base可以表示父类,但我更想理解成基类,因为更原始更具象,既 然是类,那么他就符合面向对象的设计规则和特点,我们知道面向对象的三个特点是封装 ...

  8. 【codeforces 792C】Divide by Three

    [题目链接]:http://codeforces.com/contest/792/problem/C [题意] 让你删掉最少的数字使得剩下的数字%3==0 [题解] 看代码..内置题解了现在. [完整 ...

  9. 【[Offer收割]编程练习赛12 B】一面砖墙

    [题目链接]:http://hihocoder.com/problemset/problem/1494 [题意] [题解] 显然只要记住每一行的各个砖头的间隔处的坐标有多少个就好了: ->也就对 ...

  10. [bzoj4282]慎二的随机数列_动态规划_贪心

    慎二的随机数列 bzoj-4282 题目大意:一个序列,序列上有一些数是给定的,而有一些位置上的数可以任意选择.问最长上升子序列. 注释:$1\le n\le 10^5$. 想法:结论:逢N必选.N是 ...