MonoRail学习笔记五:定制服务实现自定义功能

 

在上一篇 MonoRail学习笔记四:MonoRail基本流程分析 中我提到,MonoRail中可以自定义一些服务。比如可以定义自己的Url解析类,来实现 http://localhost:***/index.rails 等 http://localhost:***/*.rails 的效果。
具体步骤如下:
1、修改web.config文件,在 monorail节中加入以下定义
     < 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();
            }

        }

修改后的方法:
         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();
            }

        }
红色标示的为修改部分
其实意思很简单,就是当访问 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

等很多服务,来实现特定效果
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值