Asp.net c illegal vocabulary filter

Source: Internet
Author: User
Tags foreach flush

Asp tutorial. net c illegal vocabulary filter
 
Using system;
Using system. text;
Using system. io;
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 seek (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 );
// Obtain the list of invalid words, which can be read from the database tutorial or web. config.
String pattern = @ "(illegal word 1 | illegal Word 2 | illegal word 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 add the following code to the global. asax file:
 
// This is suitable for processing multiple pages at a time
Public void application_beginrequest ()
{  
String path = httpcontext. current. request. path. tolower ();
String [] paths = new string []
    {  
"/Aa/", "/bb /"
};
Foreach (string item in paths)
    {  
If (path. startswith (item ))
        {  
Response. filter = new responsefilter (response. filter );
Break;
        }  
    }  
}  
 
Or rewrite the render method of a page.
 

Protected override void render (htmltextwriter writer)
{  
Stringwriter strwriter = new stringwriter ();
Base. render (new htmltextwriter (strwriter ));
String html = strwriter. tostring ();
Html = util. replaceinvalidkeyword (html );
Writer. write (html );
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.