MonoRail学习目录
实例代码下载:/Files/maplye/MonoRailWeb.rar
虽然已经有人写了这方面的文章,
如:流腥鱼看.net 的一个简单的MonoRail例子
但是为了阅读的完整性,我还是重新边作了新的实例边写了这篇文章。
本实例使用VS2003,实例如下:
1.到官方网站下载安装文件,地址如下:
http://www.castleproject.org/index.php/Castle:Download
目前最新版本Beta5
(您也可以不需要下载,直接使用实例代码中lib中的dll)
2.添加对
Castle.MonoRail.Framework.dll
Castle.MonoRail.Framework.Views.CompositeView.dll
Castle.MonoRail.Framework.Views.NVelocity.dll
NVelocity.dll
的引用。
若你没有下载和安装Castle,在我的实例代码的lib中有这些dll的文件
3.创建一个Web项目,并在其下创建views和controllers目录
4.在controllers目录下创建HomeController.cs,并创建Index方法,代码如下:
using
System;
using
Castle.MonoRail.Framework;
![](https://maplye.cnblogs.com/Images/OutliningIndicators/None.gif)
namespace
MonoRailTest.controllers
![ExpandedBlockStart.gif](https://maplye.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
![ExpandedSubBlockStart.gif](https://maplye.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// HomeController 的摘要说明。
/// </summary>
public class HomeController:Controller
![ExpandedSubBlockStart.gif](https://maplye.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
public HomeController()
![ExpandedSubBlockStart.gif](https://maplye.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//
// TODO: 在此处添加构造函数逻辑
//
}
![](https://maplye.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public void Index()
![ExpandedSubBlockStart.gif](https://maplye.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
PropertyBag.Add("name", "MonoRail实例");
![](https://maplye.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://maplye.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
}
![](https://maplye.cnblogs.com/Images/OutliningIndicators/None.gif)
5.在Views目录下创建home目录下,并在home目录下创建index.vm文件,代码如下:
显示信息:$name
6.配置web.config文件
<?
xml version
=
"
1.0
"
encoding
=
"
utf-8
"
?>
<
configuration
>
<
configSections
>
<
section name
=
"
monoRail
"
type
=
"
Castle.MonoRail.Framework.Configuration.MonoRailSectionHandler, Castle.MonoRail.Framework
"
/>
</
configSections
>
<!--
如果要使用Castle的IOC,则为
-->
<!--<
monoRail useWindsorIntegration
=
"
true
"
>
-->
<
monoRail
>
<
controllers
>
<!--
定义controller所在的assembly
-->
<
assembly
>
MonoRailWeb
</
assembly
>
</
controllers
>
<
viewEngine
viewPathRoot
=
"
Views
"
customEngine
=
"
Castle.MonoRail.Framework.Views.NVelocity.NVelocityViewEngine, Castle.MonoRail.Framework.Views.NVelocity
"
/>
</
monoRail
>
<
system.web
>
<
httpHandlers
>
<!--
定义httpHandler, 让MonoRailHttpHandlerFactory接管指定扩展名的url
-->
<
add verb
=
"
*
"
path
=
"
*.rails
"
type
=
"
Castle.MonoRail.Framework.MonoRailHttpHandlerFactory, Castle.MonoRail.Framework
"
/>
<!--
阻止模板文件被下载
-->
<
add verb
=
"
*
"
path
=
"
*.vm
"
type
=
"
System.Web.HttpForbiddenHandler
"
/>
</
httpHandlers
>
</
system.web
>
![](https://maplye.cnblogs.com/Images/OutliningIndicators/None.gif)
</
configuration
>
![](https://maplye.cnblogs.com/Images/OutliningIndicators/None.gif)
7.配置iis,在iis中注册.rails的扩展名,查看 http://goodlyts.cnblogs.com/archive/2005/09/27/244686.html
8.在Views目录下创建nvelocity.properties文件,内容如下:
input.encoding=GB2312
output.encoding=GB2312
这是解决模板中中文乱码的问题
9.现在就可以访问http://localhost:8089/home/index.rails,页面就会出现相应的信息了。