• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Asp.NETMVCWidget开发-Mobile支持

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

在Asp.NET开发博客类系统,我们经常都会用到Widget,像在线好友、最近访问好友、最新留言等,关于Asp.NET MVC与Asp.NET视图的差异,这里不再说了,大家可去查一下,接下来我以“我的好友”列表来要介绍在Asp.NET MVC实现这一功能以及结构设计。

  • 开发工具:VS 2010 EN
  • 开发语言:Visual C#
  • ASP.NET MVC 3
  • Windows Phone 7 Emulator
  1. Asp.NET MVC Widget - 设计
  2. Asp.NET MVC Widget - Controller控制器
  3. Asp.NET MVC Widget - ViewEngine
  4. Asp.NET MVC Widget - Mobile支持
  5. Asp.NET MVC Widget - Html.Widget扩展方法

前3篇中已经讲了Widget实现方法,并用“我的好友”实例做了演示,接下来将要支持Mobile了。

1. 先来贴下"widgets"的目录结构

/Mobile                                                存放Mobile支持的页面文件

/Mobile/Widget.cshtml                          通用的Mobile显示页面,即任何手机客户端未找到情况下,调用此显示

/Mobile/iPhone/                                    存放针对iPhone类型文件

/Mobile/iPhone/Widget.cshtml                iPhone主页面文件

/Mobile/WindowsMobile                         存放针对WindowsMobile类型文件

/Mobile/WindowsMobile/Widget.cshtml    WindowsMobile主页面文件

2. 修改视图引擎以支持Mobile

打开我们之前添加的WidgetViewEngine.cs,添加对Mobile支持

  • 添加public StringDictionary Devices { get; set; },存储设备对应的目录
  • 重载FindPartialView(ControllerContext controllerContext, string partialViewName, bool useCache)方法
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Collections.Specialized;

namespace Widgets
{
public class WidgetViewEngine : BuildManagerViewEngine
{
internal static readonly string ViewStartFileName = "_ViewStart";

public StringDictionary Devices { get; set; }

public WidgetViewEngine()
:
this(null)
{
}

public WidgetViewEngine(IViewPageActivator viewPageActivator)
:
base(viewPageActivator)
{
AreaViewLocationFormats
= new[] {
"~/Areas/{2}/Views/{1}/{0}.cshtml",
"~/Areas/{2}/Views/Shared/{0}.cshtml"
};
AreaMasterLocationFormats
= new[] {
"~/Areas/{2}/Views/{1}/{0}.cshtml",
"~/Areas/{2}/Views/Shared/{0}.cshtml"
};
AreaPartialViewLocationFormats
= new[] {
"~/Areas/{2}/Views/{1}/{0}.cshtml",
"~/Areas/{2}/Views/Shared/{0}.cshtml"
};

ViewLocationFormats
= new[] {
"~/Views/{1}/{0}.cshtml",
"~/Views/Shared/{0}.cshtml"

};
MasterLocationFormats
= new[] {
"~/Views/{1}/{0}.cshtml",
"~/Views/Shared/{0}.cshtml"
};
PartialViewLocationFormats
= new[] {
"~/{1}s/{0}/Widget.cshtml",
"~/Views/{1}/{0}.cshtml",
"~/Views/Shared/{0}.cshtml"
};

// 初使化Mobile对应文件目录
Devices = new StringDictionary
{
{
"IEMobile","WindowsMobile"},
{
"Pocket IE","WindowsMobile"},
{
"AppleMAC-Safari","iPhone"}
};

FileExtensions
= new[] {
"cshtml"
};
}

protected override IView CreatePartialView(ControllerContext controllerContext, string partialPath)
{
return new RazorView(controllerContext, partialPath,
layoutPath:
null, runViewStartPages: false, viewStartFileExtensions: FileExtensions, viewPageActivator: ViewPageActivator);
}

protected override IView CreateView(ControllerContext controllerContext, string viewPath, string masterPath)
{
var view
= new RazorView(controllerContext, viewPath,
layoutPath: masterPath, runViewStartPages:
true, viewStartFileExtensions: FileExtensions, viewPageActivator: ViewPageActivator);
return view;
}

public override ViewEngineResult FindPartialView(ControllerContext controllerContext, string partialViewName, bool useCache)
{
ViewEngineResult result
= null;
var request
= controllerContext.HttpContext.Request;
var isMobile
= request.Browser.IsMobileDevice;

if (isMobile && controllerContext.IsChildAction)
{
var device
= request.Browser.Browser;

result
= base.FindPartialView(
controllerContext,
string.Format("{1}/Mobile/{0}", Devices[device], partialViewName),
useCache);
if ((result == null || result.View == null) && isMobile)
result
= base.FindPartialView(
controllerContext,
string.Format("{0}/Mobile", partialViewName),
useCache);
}

if (result == null || result.View == null)
result
= base.FindPartialView(controllerContext, partialViewName, useCache);

return result;
}

public override ViewEngineResult FindView(ControllerContext controllerContext, string viewName, string masterName, bool useCache)
{
ViewEngineResult result
= null;
var request
= controllerContext.HttpContext.Request;
var isMobile
= request.Browser.IsMobileDevice;

if (isMobile)
{
var device
= request.Browser.Browser;

result
= base.FindView(
controllerContext,
string.Format("Mobile/{0}/{1}", Devices[device], viewName),
masterName,
useCache);
if ((result == null || result.View == null) && isMobile)
result
= base.FindView(
controllerContext,
string.Format("Mobile/{0}", viewName),
masterName,
useCache);
}

if (result == null || result.View == null)
result
= base.FindView(controllerContext, viewName, masterName, useCache);

return result;
}
}
}

3. 更改显示页面

/Mobile/Widget.cshtml

@model IEnumerable<Widgets.Models.Friend>
@{
Layout = "~/widgets/_Layout.cshtml";
ViewBag.Title = "My friends";
}
<div class="newsletter">
<ul>
@foreach (var item in Model)
{
<li>@item.Name</li>
}
</ul>
</div>

/Mobile/iPhone/Widget.cshtml

@model IEnumerable<Widgets.Models.Friend>
@{
Layout = "~/widgets/_Layout.cshtml";
ViewBag.Title = "My friends";
}
<div class="newsletter">
<ul>
@foreach (var item in Model)
{
<li>@item.Name</li>
}
</ul>
<span>来自iPhone手机客户端</span>
</div>

/Mobile/WindowsMobile/Widget.cshtml

@model IEnumerable<Widgets.Models.Friend>
@{
Layout = "~/widgets/_Layout.cshtml";
ViewBag.Title = "My friends";
}
<div class="newsletter">
<ul>
@foreach (var item in Model)
{
<li>@item.Name</li>
}
</ul>
<span>来自windows mobile手机客户端</span>
</div>

4. 使用Windows Phone 7 Emulator打开页面


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
ASP.NET快速学习方案(.NET菜鸟的成长之路)发布时间:2022-07-10
下一篇:
.NetASP.NET打开指定文件夹发布时间:2022-07-10
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap