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!