asp.net use Response.filter to filter illegal vocabulary _ practical skills

Source: Internet
Author: User
Another solution is to filter out the very words in the output the advantage is that as long as you write once, you can filter the whole station illegal words, the disadvantage is that the illegal vocabulary is still deposited into the database, oh, we can have targeted choice, this example uses the latter, the cause is that the original did not do this function, and then need to add, At this time and do not want to change the original code, so think of this approach, mainly using the Httpresponse.filter property to deal with. The specific code is as follows:

First, customize a class to act as a filter for illegal words.
Copy Code code as follows:

public class Responsefilter:stream
{
#region Properties
Stream Responsestream;
Long position;
StringBuilder html = new StringBuilder ();
#endregion
#region Constructor
Public Responsefilter (Stream InputStream)
{
Responsestream = InputStream;
}
#endregion
#region implemented abstract members
public override bool CanRead
{
get {return true;}
}
public override bool CanSeek
{
get {return true;}
}
public override bool CanWrite
{
get {return true;}
}
public override void Close ()
{
Responsestream.close ();
}
public override void Flush ()
{
Responsestream.flush ();
}
public override Long Length
{
get {return 0;}
}
public override Long Position
{
get {return position;}
set {position = value;}
}
public override long (long offset, System.IO.SeekOrigin direction)
{
return Responsestream.seek (offset, direction);
}
public override void SetLength (long length)
{
Responsestream.setlength (length);
}
public override int Read (byte[] buffer, int offset, int count)
{
Return responsestream.read (buffer, offset, count);
}
#endregion
#region Write method
public override void Write (byte[] buffer, int offset, int count)
{
String sbuffer = System.Text.UTF8Encoding.UTF8.GetString (buffer, offset, count);
Get a list of illegal words that can be read in a database or web.config
String pattern = @ "(illegal words 1| illegal words 2| illegal words 3)";
String[] s = pattern. Split (new string[] {"|"}, stringsplitoptions.removeemptyentries);
foreach (string S1 in s)
{
Sbuffer = Sbuffer.replace (S1, "* *");
}
byte[] data = System.Text.UTF8Encoding.UTF8.GetBytes (Sbuffer);
Responsestream.write (data, 0, data.) Length);
}
#endregion
}

Then global.asax the file and add the following code:
[Code]
public void Application_BeginRequest () {
Response.filter = new Responsefilter (response.filter);
}
OK, test it!
Related Article

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.