Use Response.Filterは、繁体字変換のための静的ページ生成を簡略化する.UrlRewrite,asp.Net動的生成htmページ
7763 ワード
//Response.Filterは実際にはStreamタイプで書くことができます.ページの不正文字の削除、簡体字の繁体字変換、静的ページ生成など、クライアントに最終的にページを提示する前に処理することができます.
下を見てください
using
System;
using
System.Data;
using
System.Configuration;
using
System.Linq;
using
System.Web;
using
System.Web.Security;
using
System.Web.UI;
using
System.Web.UI.HtmlControls;
using
System.Web.UI.WebControls;
using
System.Web.UI.WebControls.WebParts;
using
System.Xml.Linq;
using
System.IO;
using
System.Text;
///
///
Summary description for ResponseFilter
///
public
class
ResponseFilter:Stream{ Stream newStream;
long
position; StringBuilder strb
=
new
StringBuilder();
public
ResponseFilter() {
//
//
TODO: Add constructor logic here
//
}
public
ResponseFilter(Stream inputStream) { newStream
=
inputStream;
//
//
TODO: Add constructor logic here
//
}
public
override
bool
CanRead {
get
{
return
true
; } }
public
override
bool
CanSeek {
get
{
return
true
; } }
public
override
bool
CanWrite {
get
{
return
true
; } }
public
override
void
Flush() { newStream.Flush(); }
public
override
long
Length {
get
{
return
0
; } }
public
override
long
Position {
get
{
return
position; }
set
{ position
=
value; } }
public
override
int
Read(
byte
[] buffer,
int
offset,
int
count) {
return
newStream.Read(buffer, offset, count); }
public
override
long
Seek(
long
offset, SeekOrigin origin) {
return
newStream.Seek(offset, origin); }
public
override
void
SetLength(
long
value) { newStream.SetLength(value); }
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);
//
newStream.Write(data, 0, data.Length);
//
----------------------------------------------------------------
string
s
=
Microsoft.VisualBasic.Strings.StrConv( Encoding.GetEncoding(
"
GB2312
"
).GetString(buffer), Microsoft.VisualBasic.VbStrConv.TraditionalChinese,
0
);
byte
[] data
=
Encoding.GetEncoding(
"
BIG5
"
).GetBytes(s); newStream.Write(data,
0
, data.Length); }}
//Call the method.
void
Application_BeginRequest(
object
sender, EventArgs e) {
//
Context.Response.Charset = "BIG5";
//
Context.Response.Buffer = false;
Context.Response.Filter
=
new
ResponseFilter(Response.Filter); }
protected
void
Button1_Click(
object
sender, EventArgs e) {
//
Response.Filter=new GZipStream(Response.Filter, CompressionMode.Compress);
//
Response.AddHeader("content-encoding","gzip");
}
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);}
UrlRewrite,asp.Net動の生成htmページ
下を見てください
using
System;
using
System.Data;
using
System.Configuration;
using
System.Linq;
using
System.Web;
using
System.Web.Security;
using
System.Web.UI;
using
System.Web.UI.HtmlControls;
using
System.Web.UI.WebControls;
using
System.Web.UI.WebControls.WebParts;
using
System.Xml.Linq;
using
System.IO;
using
System.Text;
///
///
Summary description for ResponseFilter
///
public
class
ResponseFilter:Stream{ Stream newStream;
long
position; StringBuilder strb
=
new
StringBuilder();
public
ResponseFilter() {
//
//
TODO: Add constructor logic here
//
}
public
ResponseFilter(Stream inputStream) { newStream
=
inputStream;
//
//
TODO: Add constructor logic here
//
}
public
override
bool
CanRead {
get
{
return
true
; } }
public
override
bool
CanSeek {
get
{
return
true
; } }
public
override
bool
CanWrite {
get
{
return
true
; } }
public
override
void
Flush() { newStream.Flush(); }
public
override
long
Length {
get
{
return
0
; } }
public
override
long
Position {
get
{
return
position; }
set
{ position
=
value; } }
public
override
int
Read(
byte
[] buffer,
int
offset,
int
count) {
return
newStream.Read(buffer, offset, count); }
public
override
long
Seek(
long
offset, SeekOrigin origin) {
return
newStream.Seek(offset, origin); }
public
override
void
SetLength(
long
value) { newStream.SetLength(value); }
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);
//
newStream.Write(data, 0, data.Length);
//
----------------------------------------------------------------
string
s
=
Microsoft.VisualBasic.Strings.StrConv( Encoding.GetEncoding(
"
GB2312
"
).GetString(buffer), Microsoft.VisualBasic.VbStrConv.TraditionalChinese,
0
);
byte
[] data
=
Encoding.GetEncoding(
"
BIG5
"
).GetBytes(s); newStream.Write(data,
0
, data.Length); }}
//Call the method.
void
Application_BeginRequest(
object
sender, EventArgs e) {
//
Context.Response.Charset = "BIG5";
//
Context.Response.Buffer = false;
Context.Response.Filter
=
new
ResponseFilter(Response.Filter); }
protected
void
Button1_Click(
object
sender, EventArgs e) {
//
Response.Filter=new GZipStream(Response.Filter, CompressionMode.Compress);
//
Response.AddHeader("content-encoding","gzip");
}
//
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