另一种解决方案是在输出时过滤掉非常词汇,优点是只要写一次就好了,可以过滤整站的非法词汇,缺点是,非法词汇仍然存入到了数据库中,呵呵,大家可以有针对性的选择,本例用的是后者,起因在于当初没有做此功能,后来需要添加,这时又不想改原来代码,所以就想了这个办法,主要是采用了HttpResponse.Filter属性来处理。具体代码如下:
首先自定义一个类,来作为非法词汇的过滤器
复制代码 代码如下:
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文件中,添加如下代码:
[code]
public void Application_BeginRequest(){
Response.Filter = new ResponseFilter(Response.Filter);
}
OK,测试一下吧!
首先自定义一个类,来作为非法词汇的过滤器
复制代码 代码如下:
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文件中,添加如下代码:
[code]
public void Application_BeginRequest(){
Response.Filter = new ResponseFilter(Response.Filter);
}
OK,测试一下吧!
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
桃源资源网 Design By www.nqtax.com
暂无“asp.net 使用Response.Filter 过滤非法词汇”评论...
RTX 5090要首发 性能要翻倍!三星展示GDDR7显存
三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。
首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。
据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。