asp教程.net c非法词汇的过滤器
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);
//得到非法词汇列表,这个可以在数据库教程或web.config中读取出来
string pattern = @"(非法词汇1|非法词汇2|非法词汇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
}
然后再global.asax文件中,添加如下代码:
//这个适合多个页一次过处理
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;
}
}
}
或者重写某页的render方法
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);
}
创造与魔法 安卓版v1.0.0750
创造与魔法是一款开放世界手游,在游戏中玩家可探索这个奇妙的世
创造与魔法修改版 最新版v1.0.0750
创造与魔法无限点券版是款探索冒险游戏,该款游戏的操作还是蛮自
战争与文明官方版本 安卓版v1.7.16
战争与文明是一款由上海邮通科技有限公司开发的战争策略游戏,这
迷你世界0元领皮肤无限迷你币版 最新安卓版v1.43.0
迷你世界0元购买皮肤版是这款开放沙盒冒险建造游戏的特殊破解版
创造与魔法无限经验版 安卓版v1.0.0750
创造与魔法无限经验版是款可以改造环境,整个游戏的自由度还是蛮