下面就是通过一个最最简单的例子来理解一下.net 下面怎么实现mvc模式的。
(1)首先新建一个"MonoRailTest"的web项目。去官方网站下载MonoRail beta4 最新版本http://sourceforge.net/project/showfiles.php?group_id=124416,然后在项目中添加下面引用
Castle.MonoRail.Engine.dll
Castle.MonoRail.Framework.dll
Castle.MonoRail.Framework.Views.NVelocity.dll
Castle.MonoRail.Framework.Views.CompositeView.dll
NVelocity.dll (NVelocity template engine)
log4net.dll (Required by NVelocity)
Commons.dll (Required by NVelocity)
(2)在项目中建3个文件夹
Controllers: 控制器
Models: 模型
Views: 视图
(注:不一定要按照这种规范文件夹名可以由自己定义)
(3)在Controllers 文件夹下面新建一个HomeController.cs 文件
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](/Images/OutliningIndicators/None.gif)
(4)对应上面的控制器代码新建一个视图文件放到Views下面文件名为index.vm,(在MonoRail下面即支持NVelocity也支持aspx,不过建议用Nvelocity,使用起来比较简单。)
Nvelocity的语法可以查看http://jakarta.apache.org/velocity/docs/vtl-reference-guide.html
index.vm
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
OK,通过这个页面就可以把控制器里面的“name”属性显示到页面上了。
(5)当然,最后想在浏览器上面显示还需要一些配置。.
配置Web.config
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
配置iis,在iis中注册.rails的扩展名,具体步骤参考我上篇随笔 http://goodlyts.cnblogs.com/archive/2005/09/27/244686.html
在浏览器打上http://localhost/MonoRailTest/Home/index.rails就可以看到页面上显示
“Hi there, my name is John Doe !”
属性“name”被显示到页面上了,是不是很简单呢?
附例子的源代码下载 https://files.cnblogs.com/goodlyts/MonoRailTest.zip