具体步骤如下:
1、修改web.config文件,在monorail节中加入以下定义
<services>
<service id="UrlTokenizer" type="TestSiteNVelocity.CustomUrlTokenizer, TestSiteNVelocity" />
</services>
<service id="UrlTokenizer" type="TestSiteNVelocity.CustomUrlTokenizer, TestSiteNVelocity" />
</services>
2、编写自己的CustomUrlTokenizer类
这里为了方便,我直接复制默认的Castle.MonoRail.Framework.Services.DefaultUrlTokenizer类,将复制好的类放入TestSiteNVelocity,改名为CustomUrlTokenizer,然后在此基础上修改。
当然,这个类中有很多方法,也就可以自定义很多功能,为了实现http://localhost:***/*.rails 的效果,我们只需要修改ExtractAreaControllerAction方法。
原方法:
private void ExtractAreaControllerAction(string rawUrl, out string area, out string controller, out string action)
{
string[] parts = rawUrl.Split('/');
if (parts.Length < 2)
{
throw new UrlTokenizerException("Url smaller than 2 tokens");
}
action = parts[parts.Length - 1];
int fileNameIndex = action.IndexOf('.');
if (fileNameIndex != -1)
{
action = action.Substring(0, fileNameIndex);
}
controller = parts[parts.Length - 2];
area = string.Empty;
if (parts.Length - 3 == 0)
{
area = parts[parts.Length - 3];
}
else if (parts.Length - 3 > 0)
{
StringBuilder areaSB = new StringBuilder();
for(int i = 0; i <= parts.Length - 3; i++)
{
if (parts[i] != null && parts[i].Length > 0)
{
areaSB.Append(parts[i]).Append('/');
}
}
if (areaSB.Length > 0)
{
areaSB.Length -= 1;
}
area = areaSB.ToString();
}
}
{
string[] parts = rawUrl.Split('/');
if (parts.Length < 2)
{
throw new UrlTokenizerException("Url smaller than 2 tokens");
}
action = parts[parts.Length - 1];
int fileNameIndex = action.IndexOf('.');
if (fileNameIndex != -1)
{
action = action.Substring(0, fileNameIndex);
}
controller = parts[parts.Length - 2];
area = string.Empty;
if (parts.Length - 3 == 0)
{
area = parts[parts.Length - 3];
}
else if (parts.Length - 3 > 0)
{
StringBuilder areaSB = new StringBuilder();
for(int i = 0; i <= parts.Length - 3; i++)
{
if (parts[i] != null && parts[i].Length > 0)
{
areaSB.Append(parts[i]).Append('/');
}
}
if (areaSB.Length > 0)
{
areaSB.Length -= 1;
}
area = areaSB.ToString();
}
}
修改后的方法:
private void ExtractAreaControllerAction(string rawUrl, out string area, out string controller, out string action)
{
string[] parts = rawUrl.Split('/');
action = parts[parts.Length - 1];
int fileNameIndex = action.IndexOf('.');
if (fileNameIndex != -1)
{
action = action.Substring(0, fileNameIndex);
}
if (parts.Length < 2)
{
controller = "servlet"; ;
area = "";
return;
}
controller = parts[parts.Length - 2];
area = string.Empty;
if (parts.Length - 3 == 0)
{
area = parts[parts.Length - 3];
}
else if (parts.Length - 3 > 0)
{
StringBuilder areaSB = new StringBuilder();
for(int i = 0; i <= parts.Length - 3; i++)
{
if (parts[i] != null && parts[i].Length > 0)
{
areaSB.Append(parts[i]).Append('/');
}
}
if (areaSB.Length > 0)
{
areaSB.Length -= 1;
}
area = areaSB.ToString();
}
}
红色标示的为修改部分{
string[] parts = rawUrl.Split('/');
action = parts[parts.Length - 1];
int fileNameIndex = action.IndexOf('.');
if (fileNameIndex != -1)
{
action = action.Substring(0, fileNameIndex);
}
if (parts.Length < 2)
{
controller = "servlet"; ;
area = "";
return;
}
controller = parts[parts.Length - 2];
area = string.Empty;
if (parts.Length - 3 == 0)
{
area = parts[parts.Length - 3];
}
else if (parts.Length - 3 > 0)
{
StringBuilder areaSB = new StringBuilder();
for(int i = 0; i <= parts.Length - 3; i++)
{
if (parts[i] != null && parts[i].Length > 0)
{
areaSB.Append(parts[i]).Append('/');
}
}
if (areaSB.Length > 0)
{
areaSB.Length -= 1;
}
area = areaSB.ToString();
}
}
其实意思很简单,就是当访问http://localhost:***/*.rails 形式的页面时,默认的controller类为ServletController类
接下来就可以按常规方式编写ServletController类和view部分
这样之后当我们访问http://localhost:***/index.rails时,调用的就是ServletCOntroller类的Index方法,当访问http://localhost:***/bag.rails 时,调用的就是ServletCOntroller类的Bag方法 ......
当然我们也同样可以定义
[DefaultAction("Index")]
public class ServletController : Controller
让所有未定义的http://localhost:***/*.rails访问调用ServletController的Index方法
这篇文章只是涉及了自定义服务的很小的一个点,我们还可以自定义:
ControllerFactory
ViewComponentFactory
ResourceFactory
EmailSender
EmailTemplateService
UrlBuilder
ValidatorRegistry
等很多服务,来实现特定效果
本文转自永春博客园博客,原文链接:http://www.cnblogs.com/firstyi/archive/2007/10/22/932768.html,如需转载请自行联系原作者