下面就是通过一个最最简单的例子来理解一下.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 文件
using
System;
using Castle.MonoRail.Framework;
namespace MonoRailTest.Controllers
{
public class HomeController :Controller
{
public void Index()
{
PropertyBag.Add("name", "John Doe");
}
}
}
using Castle.MonoRail.Framework;
namespace MonoRailTest.Controllers
{
public class HomeController :Controller
{
public void Index()
{
PropertyBag.Add("name", "John Doe");
}
}
}
(4)对应上面的控制器代码新建一个视图文件放到Views下面文件名为index.vm,(在MonoRail下面即支持NVelocity也支持aspx,不过建议用Nvelocity,使用起来比较简单。)
Nvelocity的语法可以查看http://jakarta.apache.org/velocity/docs/vtl-reference-guide.html
index.vm
<
html
>
< body >
Hi there, my name is $name! </ body >
</ html >
< body >
Hi there, my name is $name! </ body >
</ html >
配置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”被显示到页面上了,是不是很简单呢?
附例子的源代码下载 http://www.cnblogs.com/Files/goodlyts/MonoRailTest.zip
OK,通过这个页面就可以把控制器里面的“name”属性显示到页面上了。
(5)当然,最后想在浏览器上面显示还需要一些配置。.
配置 Web.config
配置 Web.config
<?
xml version="1.0" encoding="utf-8"
?>
< configuration >
< configSections >
< section name ="monoRail"
type ="Castle.MonoRail.Engine.Configuration.MonoRailSectionHandler, Castle.MonoRail.Engine" />
</ configSections >
< monoRail >
< controllers >
< assembly > MonoRailTest </ assembly >
</ controllers >
< viewEngine
viewPathRoot ="Views"
customEngine ="Castle.MonoRail.Framework.Views.CompositeView.CompositeViewEngine, Castle.MonoRail.Framework.Views.CompositeView" />
</ monoRail >
< system .web >
< httpHandlers >
< add verb ="*" path ="*.rails"
type ="Castle.MonoRail.Engine.MonoRailHttpHandlerFactory, Castle.MonoRail.Engine" />
<!-- prevent downloads of your views scripts code -->
< add verb ="*" path ="*.vm" type ="System.Web.HttpForbiddenHandler" />
< add verb ="*" path ="*.boo" type ="System.Web.HttpForbiddenHandler" />
</ httpHandlers >
</ system.web >
</ configuration >
< configuration >
< configSections >
< section name ="monoRail"
type ="Castle.MonoRail.Engine.Configuration.MonoRailSectionHandler, Castle.MonoRail.Engine" />
</ configSections >
< monoRail >
< controllers >
< assembly > MonoRailTest </ assembly >
</ controllers >
< viewEngine
viewPathRoot ="Views"
customEngine ="Castle.MonoRail.Framework.Views.CompositeView.CompositeViewEngine, Castle.MonoRail.Framework.Views.CompositeView" />
</ monoRail >
< system .web >
< httpHandlers >
< add verb ="*" path ="*.rails"
type ="Castle.MonoRail.Engine.MonoRailHttpHandlerFactory, Castle.MonoRail.Engine" />
<!-- prevent downloads of your views scripts code -->
< add verb ="*" path ="*.vm" type ="System.Web.HttpForbiddenHandler" />
< add verb ="*" path ="*.boo" type ="System.Web.HttpForbiddenHandler" />
</ httpHandlers >
</ system.web >
</ configuration >