在上一篇
MonoRail学习笔记四:MonoRail基本流程分析 中我提到,MonoRail中可以自定义一些服务。比如可以定义自己的Url解析类,来实现
http://localhost:***/index.rails等
http://localhost:***/*.rails 的效果。
具体步骤如下:
1、修改web.config文件,在 monorail节中加入以下定义
2、编写自己的 CustomUrlTokenizer类
这里为了方便,我直接复制默认的 Castle.MonoRail.Framework.Services.DefaultUrlTokenizer类,将复制好的类放入TestSiteNVelocity,改名为CustomUrlTokenizer,然后在此基础上修改。
当然,这个类中有很多方法,也就可以自定义很多功能,为了实现http://localhost:***/*.rails 的效果,我们只需要修改ExtractAreaControllerAction方法。
原方法:
修改后的方法:
其实意思很简单,就是当访问 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
等很多服务,来实现特定效果
具体步骤如下:
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方法。
原方法:
privatevoidExtractAreaControllerAction(stringrawUrl,outstringarea,outstringcontroller,outstring
action)
{
string[]parts=rawUrl.Split('/');
if(parts.Length<2)
{
thrownewUrlTokenizerException("Urlsmallerthan2tokens");
}
action=parts[parts.Length-1];
intfileNameIndex=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];
}
elseif(parts.Length-3>0)
{
StringBuilderareaSB=newStringBuilder();
for(inti=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)
{
thrownewUrlTokenizerException("Urlsmallerthan2tokens");
}
action=parts[parts.Length-1];
intfileNameIndex=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];
}
elseif(parts.Length-3>0)
{
StringBuilderareaSB=newStringBuilder();
for(inti=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();
}
}
修改后的方法:
privatevoidExtractAreaControllerAction(stringrawUrl,outstringarea,outstringcontroller,outstring
action)
{
string[]parts=rawUrl.Split('/');
action=parts[parts.Length-1];
intfileNameIndex=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];
}
elseif(parts.Length-3>0)
{
StringBuilderareaSB=newStringBuilder();
for(inti=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];
intfileNameIndex=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];
}
elseif(parts.Length-3>0)
{
StringBuilderareaSB=newStringBuilder();
for(inti=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
等很多服务,来实现特定效果