Monorail Study Notes 17: Use of transformfilter

Source: Internet
Author: User
Transformfilter is used for conversion. For example, it converts all the content on the page to uppercase or converts some keywords to hyperlinks. For example, we often see that some keywords in Blog content have been converted into hyperlinks, so we can use transformfilter to implement it. Transformfilter is executed before the page is actually rendered.

1. Replace lowercase letters with uppercase letters using ready-made classes:
Controller: [Transformfilter ( Typeof (Wikitransformfilter), executionorder =   1 ), Transformfilter ( Typeof (Uppercasetransformfilter), executionorder =   2 )]
Public   Void Index ()
{
}

The transformfilter attribute is used, and then the wikitransformfilter and uppercasetransformfilter filters provided in monorail are used (under Castle. Monorail. Framework. transformfilters)

You can see that multiple transformfilters can be used for the same method, and executionorder specifies the execution sequence.
Note: The transformfilter attribute can only be used on methods and cannot be used on classes.

2. Define and use your own filter class

[Transformfilter ( Typeof (Mytransformfilter)]
Public   Void Index ()
{
}

Class Mytransformfilter: transformfilter
{
Public Mytransformfilter (Stream basestream)
: Base (Basestream)
{
}

Public   Override   Void Write ( Byte [] Buffer, Int Offset, Int Count)
{
If (Closed) Throw   New Objectdisposedexception ( " Mytransformfilter " );

String Content = Encoding. Default. getstring (buffer, offset, count );

Content = Content. Replace ( " Gspring " , " <A href = 'HTTP: // www.cnblogs.com/firstyi'> Yongchun pavilion </a> " );

Byte [] Output = Encoding. Default. getbytes (content );
Basestream. Write (output, 0 , Output. Length );
}
}

Defines a mytransformfilter that inherits from transformfilter and reloads the write method.
This class is used to replace the gspring that appears on all webpages with the link of my blog -_-

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.