[转]Asp.NET MVC Widget开发 - ViewEngine
2015-12-02 15:26
736 查看
本文转自:/article/5622752.html
在Asp.NET开发博客类系统,我们经常都会用到Widget,像在线好友、最近访问好友、最新留言等,关于Asp.NET MVC与Asp.NET视图的差异,这里不再说了,大家可去查一下,接下来我以“我的好友”列表来要介绍在Asp.NET MVC实现这一功能以及结构设计。
开发工具:VS 2010 EN
开发语言:Visual C#
ASP.NET MVC 3
Asp.NET MVC Widget - 设计
Asp.NET MVC Widget - Controller控制器
Asp.NET MVC Widget - ViewEngine
Asp.NET MVC Widget - Mobile支持
Asp.NET MVC Widget - Html.Widget扩展方法
关于ViewEngine这篇是Widgets实现的核心,这里需要自定义Asp.NET MVC的视图引擎,也就是让Asp.NET MVC视图引擎可以找到widgets中的文件。
直接访问会出现以下错误,提示找不到显示文件
![](http://pic002.cnblogs.com/images/2011/101239/2011030212124036.jpg)
由错误中也可以看到 Asp.NET MVC默认引擎搜索文件的目录
/Views/Widget/
/Views/Shared/
要寻找到"widgets"中的文件就必须要让视图引擎去"widgets"中搜索文件
如Friends: /widgets/Friends/
添加自定义视图引擎
1. 添加 "WidgetViewEngine.cs",继承自BuildManagerViewEngine
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc;
namespace Widgets { public class WidgetViewEngine : BuildManagerViewEngine { internal static readonly string ViewStartFileName = "_ViewStart";
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" };
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; } } }
2. 添加AppStart_Widgets.cs,修改默认视图引擎
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using Widgets;
[assembly: PreApplicationStartMethod(typeof(AppStart_Widgets), "Start")]
namespace Widgets { public class AppStart_Widgets { public static void Start() { // Clear system default view engines ViewEngines.Engines.Clear(); ViewEngines.Engines.Add(new WidgetViewEngine()); } } }
3. 添加Friend实体及测试数据
using System; using System.Collections.Generic; using System.Linq; using System.Web;
namespace Widgets.Models { public class Friend { public int Id { get; set; } public string Name { get; set; } }
public class FriendsContext { public FriendsContext() { Friends = new List<Friend>() { new Friend{Id=1,Name="zhang san"}, new Friend{Id=2,Name="li si"}, new Friend{Id=3,Name="wang wu"} }; }
public IEnumerable<Friend> Friends { get; private set; } } }
4. 更改widgets/Friends/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>
项目结构
![](http://pic002.cnblogs.com/images/2011/101239/2011030213265116.jpg)
其它代码详见源码中。
再次访问,便出现要的结果。
![](http://pic002.cnblogs.com/images/2011/101239/2011030212442223.jpg)
源码下载
在Asp.NET开发博客类系统,我们经常都会用到Widget,像在线好友、最近访问好友、最新留言等,关于Asp.NET MVC与Asp.NET视图的差异,这里不再说了,大家可去查一下,接下来我以“我的好友”列表来要介绍在Asp.NET MVC实现这一功能以及结构设计。
开发工具:VS 2010 EN
开发语言:Visual C#
ASP.NET MVC 3
Asp.NET MVC Widget - 设计
Asp.NET MVC Widget - Controller控制器
Asp.NET MVC Widget - ViewEngine
Asp.NET MVC Widget - Mobile支持
Asp.NET MVC Widget - Html.Widget扩展方法
关于ViewEngine这篇是Widgets实现的核心,这里需要自定义Asp.NET MVC的视图引擎,也就是让Asp.NET MVC视图引擎可以找到widgets中的文件。
直接访问会出现以下错误,提示找不到显示文件
![](http://pic002.cnblogs.com/images/2011/101239/2011030212124036.jpg)
由错误中也可以看到 Asp.NET MVC默认引擎搜索文件的目录
/Views/Widget/
/Views/Shared/
要寻找到"widgets"中的文件就必须要让视图引擎去"widgets"中搜索文件
如Friends: /widgets/Friends/
添加自定义视图引擎
1. 添加 "WidgetViewEngine.cs",继承自BuildManagerViewEngine
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc;
namespace Widgets { public class WidgetViewEngine : BuildManagerViewEngine { internal static readonly string ViewStartFileName = "_ViewStart";
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" };
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; } } }
2. 添加AppStart_Widgets.cs,修改默认视图引擎
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using Widgets;
[assembly: PreApplicationStartMethod(typeof(AppStart_Widgets), "Start")]
namespace Widgets { public class AppStart_Widgets { public static void Start() { // Clear system default view engines ViewEngines.Engines.Clear(); ViewEngines.Engines.Add(new WidgetViewEngine()); } } }
3. 添加Friend实体及测试数据
using System; using System.Collections.Generic; using System.Linq; using System.Web;
namespace Widgets.Models { public class Friend { public int Id { get; set; } public string Name { get; set; } }
public class FriendsContext { public FriendsContext() { Friends = new List<Friend>() { new Friend{Id=1,Name="zhang san"}, new Friend{Id=2,Name="li si"}, new Friend{Id=3,Name="wang wu"} }; }
public IEnumerable<Friend> Friends { get; private set; } } }
4. 更改widgets/Friends/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>
项目结构
![](http://pic002.cnblogs.com/images/2011/101239/2011030213265116.jpg)
其它代码详见源码中。
再次访问,便出现要的结果。
![](http://pic002.cnblogs.com/images/2011/101239/2011030212442223.jpg)
源码下载
相关文章推荐
- [转]让Windows Server 2008 + IIS 7+ ASP.NET 支持10万并发请求
- Asp.Net 之 Web.config下Authorization节点
- [Windows Server 2008] 查看ASP详细错误信息方法
- asp.net DataTable 进行表内排序
- Mac企业应用,苹果准备好了,你呢?
- Asp.net mvc项目架构分享系列之架构概览
- Aspose.Words组件介绍及使用—基本介绍与DOM概述 (转载)
- ASP.NET防用户重复登录的方法
- Metasploit 工具(pattern_create.rb)使用前设置
- ASP.NET Calendar 控件
- Spring三种切面 <aop:aspectj-autoproxy/><aop:config></aop:config> <aop:scoped-proxy/>
- ASP.NET通用权限验证的实现代码思路
- Raspberry Pi 2 ubuntu wiringPi GPIO点亮LED
- asp.net实现微信公众账号接口开发教程
- ASP.NET Word转为PDF
- asp.net 操作word 权限
- ASP.NET微信开发(接口指南)
- ASP.NET微信开发(接口指南)
- asp.net实现微信公众账号接口开发教程
- ASP.NET通用权限验证的实现代码思路