快捷搜索:

Asp.NET MVC Widget开发 - Mobile支持

在Asp.NET开拓博客类系统,我们常常都邑用到Widget,像在线石友、近来造访石友、最新留言等,关于Asp.NET MVC与Asp.NET视图的差异,这里不再说了,大年夜家可去查一下,接下来我以“我的石友”列表来要先容在Asp.NET MVC实现这一功能以及布局设计。

开拓对象:VS 2010 EN

开拓说话:Visual C#

ASP.NET MVC 3

Windows Phone 7 Emulator

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

1. 先来贴下"widgets"的目录布局

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

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

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

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

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

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

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;

}

}

}

您可能还会对下面的文章感兴趣: