TransformFilter是用来转换用的,比如把页面所有的内容转换成大写,或把一些关键字转换成超链接。比如我们经常看到一些博客内容中的关键字都被转换成超链接了,就可以使用TransformFilter来实现。TransformFilter都是在实际渲染页面之前执行的
1、利用现成的类把小写替换成大写:
controller:
[TransformFilter(
typeof
(WikiTransformFilter),ExecutionOrder
=
1
),TransformFilter(
typeof
(UpperCaseTransformFilter),ExecutionOrder
=
2
)]
public
void
Index()
![](http://writeblog.csdn.net/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
}
主要就是使用了TransformFilter属性,然后利用monorail中已经提供的WikiTransformFilter和UpperCaseTransformFilter两个Filter(在
Castle.MonoRail.Framework.TransformFilters下)
可以看到对于同一个方法可以使用多个TransformFilter,ExecutionOrder是指定执行顺序
注意点:TransformFilter属性只能用在方法上,不能用在类上
2、定义和使用自己的Filter类
[TransformFilter(
typeof
(MyTransformFilter))]
public
void
Index()
![](http://writeblog.csdn.net/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
}
class
MyTransformFilter:TransformFilter
![](http://writeblog.csdn.net/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
publicMyTransformFilter(StreambaseStream)
:base(baseStream)
![](http://writeblog.csdn.net/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://writeblog.csdn.net/Images/OutliningIndicators/ContractedSubBlock.gif)
{
}
![](http://writeblog.csdn.net/Images/OutliningIndicators/InBlock.gif)
publicoverridevoidWrite(byte[]buffer,intoffset,intcount)
![](http://writeblog.csdn.net/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://writeblog.csdn.net/Images/OutliningIndicators/ContractedSubBlock.gif)
{
if(Closed)thrownewObjectDisposedException("MyTransformFilter");
![](http://writeblog.csdn.net/Images/OutliningIndicators/InBlock.gif)
stringcontent=Encoding.Default.GetString(buffer,offset,count);
![](http://writeblog.csdn.net/Images/OutliningIndicators/InBlock.gif)
content=content.Replace("GSpring","<ahref='http://www.cnblogs.com/firstyi'>永春阁</a>");
![](http://writeblog.csdn.net/Images/OutliningIndicators/InBlock.gif)
byte[]output=Encoding.Default.GetBytes(content);
BaseStream.Write(output,0,output.Length);
}
}
定义了一个MyTransformFilter,从TransformFilter继承,重载Write方法
这个类的作用就是把所有的网页中出现的GSpring替换成我的博客的链接-_-
1、利用现成的类把小写替换成大写:
controller:
![](http://writeblog.csdn.net/Images/OutliningIndicators/None.gif)
![](http://writeblog.csdn.net/Images/OutliningIndicators/None.gif)
![](http://writeblog.csdn.net/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://writeblog.csdn.net/Images/OutliningIndicators/ContractedBlock.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/b854634c0904529d4018c4c3336be836.gif)
![](http://writeblog.csdn.net/Images/OutliningIndicators/ExpandedBlockEnd.gif)
可以看到对于同一个方法可以使用多个TransformFilter,ExecutionOrder是指定执行顺序
注意点:TransformFilter属性只能用在方法上,不能用在类上
2、定义和使用自己的Filter类
![](http://writeblog.csdn.net/Images/OutliningIndicators/None.gif)
![](http://writeblog.csdn.net/Images/OutliningIndicators/None.gif)
![](http://writeblog.csdn.net/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://writeblog.csdn.net/Images/OutliningIndicators/ContractedBlock.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/b854634c0904529d4018c4c3336be836.gif)
![](http://writeblog.csdn.net/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](http://writeblog.csdn.net/Images/OutliningIndicators/None.gif)
![](http://writeblog.csdn.net/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://writeblog.csdn.net/Images/OutliningIndicators/ContractedBlock.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/b854634c0904529d4018c4c3336be836.gif)
![](http://writeblog.csdn.net/Images/OutliningIndicators/InBlock.gif)
![](http://writeblog.csdn.net/Images/OutliningIndicators/InBlock.gif)
![](http://writeblog.csdn.net/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://writeblog.csdn.net/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/b854634c0904529d4018c4c3336be836.gif)
![](http://writeblog.csdn.net/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](http://writeblog.csdn.net/Images/OutliningIndicators/InBlock.gif)
![](http://writeblog.csdn.net/Images/OutliningIndicators/InBlock.gif)
![](http://writeblog.csdn.net/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://writeblog.csdn.net/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/b854634c0904529d4018c4c3336be836.gif)
![](http://writeblog.csdn.net/Images/OutliningIndicators/InBlock.gif)
![](http://writeblog.csdn.net/Images/OutliningIndicators/InBlock.gif)
![](http://writeblog.csdn.net/Images/OutliningIndicators/InBlock.gif)
![](http://writeblog.csdn.net/Images/OutliningIndicators/InBlock.gif)
![](http://writeblog.csdn.net/Images/OutliningIndicators/InBlock.gif)
![](http://writeblog.csdn.net/Images/OutliningIndicators/InBlock.gif)
![](http://writeblog.csdn.net/Images/OutliningIndicators/InBlock.gif)
![](http://writeblog.csdn.net/Images/OutliningIndicators/InBlock.gif)
![](http://writeblog.csdn.net/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](http://writeblog.csdn.net/Images/OutliningIndicators/ExpandedBlockEnd.gif)
这个类的作用就是把所有的网页中出现的GSpring替换成我的博客的链接-_-
欢迎加入:http://www.itpob.cn/bbs