1. MonoRail的使用流程(Demo下载、官网文档参考) http://www.cnblogs.com/firstyi/archive/2007/11/21/966670.html 参考
1.1 设置模板引擎,即要解释的跟目录如:
<viewEngine viewPathRoot="Views/default" customEngine="Castle.MonoRail.Framework.Views.NVelocity.NVelocityViewEngine, Castle.MonoRail.Framework.Views.NVelocity"/>
1.2 设置路由规则如下所示:
<rule>
<pattern>/default.aspx</pattern>
<replace><![CDATA[/home/index.html]]></replace>
</rule>
这样的话URL为http://192.168.1.202/default.aspx将跳转到http://192.168.1.202/home/index.html
1.3 配置节通知,也就是说所有*.extend(文件扩展名)文件的请求将由MonoRailHttpHandlerFactory来进行处理如下所示:
<httpHandlers>
<add verb="*" path="*.html" type="Castle.MonoRail.Framework.MonoRailHttpHandlerFactory,Castle.MonoRail.Framework"/>
<!-- 阻止模板文件被下载 -->
<add verb="*" path="*.vm" type="System.Web.HttpForbiddenHandler"/>
</httpHandlers>
这样的话所有以*.html跟所有的*.vm都会交给MonoRailHttpHandlerFactory来进行处理而重新定位到物理文件。
1.4 添加路由接管如下所示:
<httpModules>
<add name="routing" type="Castle.MonoRail.Framework.RoutingModule, Castle.MonoRail.Framework"/>
<add name="monoRail" type="Castle.MonoRail.Framework.EngineContextModule, Castle.MonoRail.Framework"/>
<add name="filter" type="aqioo.Commons.FilterForbiddenWordModule, aqioo.Commons"/>
</httpModules>
2. 语法(在MonoRail下面即支持NVelocity也支持aspx,不过建议用Nvelocity,使用起来比较简单语法可以参考http://jakarta.apache.org/velocity/docs/vtl-reference-guide.html,也可以自己上博客园百度学习)
3. MonoRail中ViewComponents的使用
使用流程:首先自定义ViewComponent-->引用自定义的ViewComponent->使用参数->嵌套子节点的使用
3.1 自定义ViewComponent
要生成自己的ViewComponet必须从抽象类ViewComponent继承,继承后有三个方法可以被覆盖:
Initialize:初始化,可以在这个方法中接收传递的参数
Render:渲染实际的显示内容
SupportsSection:指定这个组件可以支持哪些子节点
最简单的一个ViewComponent:
using Castle.MonoRail.Framework; public class HeaderComponent : ViewComponent { }
当使用这个组件时会直接渲染views下的components/headercomponent/default.vm文件
当然和Controller一样,我们也可以指定一个渲染的vm文件:
using Castle.MonoRail.Framework; public class HeaderComponent : ViewComponent { public override void Render() { RenderView("otherview"); } }
这样的话就会使用views下的components/headercomponent/otherview.vm文件
3.2 使用Viewcomponent
使用viewcomponent有两种方式:普通方式和嵌套内容的方式
3.2.1 普通方式
#component(ViewComponentName)
3.2.2 嵌套内容的方式
C#代码:
public class BlockViewComponent2 : ViewComponent { public BlockViewComponent2() { } public override void Render() { Context.ContextVars["it"] = "GSpring"; Context.RenderBody(); } }
vm代码:
#blockcomponent(BlockViewComponent2)
inner content $it
#end
调用之后,显示在页面上的内容就是:
inner content GSpring
嵌套方式主要就是使用关键字:blockcomponent
Context.RenderBody()方法是用来渲染内容的,可以同时调用多次,那么所包含的内容就会显示多次(在我们项目中也有用到)如MallProductRepeatViewComponent中:
public sealed class MallProductRepeatViewComponent : AqiooViewComponent { #region .ctors public MallProductRepeatViewComponent() { } #endregion #region /// <summary> /// 文章固顶头条显示控件 /// #blockcomponent(ColumnHeadArticleViewComponent) /// $ArticleID #set($i=$i+1)$i<br/> /// #end /// 1. 频道头条:a_istop=1,order by a_arrtim /// 2. 频道固顶:a_istop=2,order by a_arrtim /// 3. 栏目头条:a_istop=3,order by a_arrtim。 /// 4. 栏目固顶:a_istop=4,order by a_arrtim。 /// 5. 子类固顶:a_istop=5,order by a_arrtim; /// </summary> public override void Render() { // 数目 ,类别 ,固顶类型 string num = ComponentParams["num"] + ""; string where = ComponentParams["where"] + ""; string xml = PageHtml.GetHtmlData("http://mall.aqioo.com/aspx/ProductService.aspx?top=" + num + "&where=" + where); if (!string.IsNullOrEmpty(xml)) { XmlDocument xmldc = new XmlDocument(); xmldc.LoadXml(xml); XmlNodeList list = xmldc.SelectNodes("products/product"); for (int i = 0;list!=null&&i < list.Count; i++) { this.Context.ContextVars["ID"] = list[i].SelectSingleNode("productid").InnerText; this.Context.ContextVars["Title"] = list[i].SelectSingleNode("title").InnerText; this.Context.ContextVars["TitleLink"] = string.Format("<a href=\"{0}\" target=\"_blank\">{1}</a>", list[i].SelectSingleNode("link").InnerText, list[i].SelectSingleNode("title").InnerText); this.Context.ContextVars["Photo"] = list[i].SelectSingleNode("image").InnerText; this.Context.ContextVars["Price"] = list[i].SelectSingleNode("price").InnerText; this.Context.ContextVars["Link"] = list[i].SelectSingleNode("link").InnerText; Context.RenderBody(); } } } #endregion }
3.3 使用参数
正如.NET的自定义控件一样,MonoRail中的自定义控件也可以从外部接收一些参数,比如显示的颜色、大小、其他参数等。
使用ComponentParams属性在Initialize方法中来接收:
using Castle.MonoRail.Framework; public class TableComponent : ViewComponent { private ICollection elements; private object border; private string style; private object cellpadding; private object cellspacing; public override void Initialize() { elements = (ICollection) ComponentParams["elements"]; border = ComponentParams["border"]; style = (String) ComponentParams["style"]; cellpadding = ComponentParams["cellpadding"]; cellspacing = ComponentParams["cellspacing"]; base.Initialize(); }
调用的时候,就可以写成这样:
#blockcomponent(TableComponent with "elements=$items" "border=0" "style=border: 1px solid black;" "cellpadding=0" "cellspacing=2") #end 或者 #component(TableComponent with "elements=$items" "border=0" "style=border: 1px solid black;" "cellpadding=0" "cellspacing=2")
3.4 嵌套子节点的使用
在ViewComponet中还可以定义多个子节点,在一些复杂的情况下比较方便,看下面的一个实例:
Contoller代码(就是定义一个list):
public void Default() { ArrayList items = new ArrayList(); items.Add("项目1"); items.Add("项目2"); items.Add("项目3"); PropertyBag.Add("items", items); }
ViewCompoent代码:
public class TableComponent : ViewComponent { private ICollection elements; private object border; private string style; private object cellpadding; private object cellspacing; public override void Initialize() { elements = (ICollection)ComponentParams["elements"]; border = ComponentParams["border"]; style = (String)ComponentParams["style"]; cellpadding = ComponentParams["cellpadding"]; cellspacing = ComponentParams["cellspacing"]; base.Initialize(); } public override void Render() { RenderText( String.Format("<table border=\"{0}\" style=\"{1}\" cellpadding=\"{2}\" cellspacing=\"{3}\">", border, style, cellpadding, cellspacing)); if (Context.HasSection("colheaders")) { Context.RenderSection("colheaders"); } if (elements != null) { int index = 0; foreach (object item in elements) { PropertyBag["index"] = ++index; PropertyBag["item"] = item; if (Context.HasSection("altitem") && index % 2 != 0) { Context.RenderSection("altitem"); } else { Context.RenderSection("item"); } } } RenderText("</table>"); } public override bool SupportsSection(string name) { return name == "colheaders" || name == "item" || name == "altitem"; } }
vm代码:
#blockcomponent(TableComponent with "elements=$items") #colheaders <tr> <th> </th> <th>Element</th> </tr> #end #item <tr> <td>$index</td> <td>$item</td> </tr> #end #altitem <tr> <td style="background-color:Red">$index</td> <td style="background-color:Red">$item</td> </tr> #end #end
显示的效果,如下图所示:

浙公网安备 33010602011771号