上周的工作要求在.net 2.0下進行網(wǎng)站的簡、繁轉(zhuǎn)換,當(dāng)然不會是用js來實現(xiàn)了。
規(guī)則:
以URL地址來決定簡繁的顯示,zh-cn/index.htm為簡體,zh-tw/index.htm為繁體。
思路很簡單,以IHttpHandler接口為基類,寫一個類,用來處理HttpHandler,用ProcessRequest方法來處理客戶端的請求。在ProcessRequest方法中獲取url值,有zh-tw就用繁體,反之用簡體。
using System; using System.IO; using System.Data; using System.Configuration; using System.Collections.Generic; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Text; using System.Text.RegularExpressions; using Microsoft.VisualBasic;
導(dǎo)入以上命名空間,注意Microsoft.VisualBasic,要先引用一下Microsoft.VisualBasic。
public class HtmlHttpHandler : IHttpHandler { //這里是定義的一個結(jié)構(gòu) ,與簡繁轉(zhuǎn)換無關(guān),主要設(shè)定url重寫的規(guī)則。 private List<RegexInfo> _regex_list = new List<RegexInfo>();
public HtmlHttpHandler() { DataSet ds = new DataSet(); ds.ReadXml(ConfigurationManager.AppSettings["RegexsXml"]); foreach (DataRow r in ds.Tables["regex"].Rows) _regex_list.Add(new RegexInfo(((string)r["b"]).Trim(), ((string)r["a"]).Trim())); }
//主方法
public void ProcessRequest(HttpContext context) { string path = context.Request.Path; //foreach (RegexInfo r in _regex_list) //path = Regex.Replace(path, r._before, r._after); //url重寫 //開始判斷并轉(zhuǎn)換 if (path.IndexOf("zh-tw") != -1) { path = path.Replace("zh-tw", "zh-cn"); //以自定義方式過濾 context.Response.Filter = new CnToTwStream(context.Response.Filter, context.Response.ContentEncoding); } context.Server.Transfer(path); }
// Override the IsReusable property. public bool IsReusable { get { return true; } } }
CnToTwStream類實現(xiàn)簡繁的轉(zhuǎn)換
class CnToTwStream : Stream { private Stream _sink; private MemoryStream _ms; private Encoding _encoding;
public CnToTwStream(Stream sink, Encoding encoding) { _sink = sink; _ms = new MemoryStream(); _encoding = encoding; }
public override bool CanRead { get { return false; } }
public override bool CanSeek { get { return false; } }
public override bool CanWrite { get { return true; } }
public override long Length { get { return _ms.Length; } }
public override long Position { get { return _ms.Position; } set { throw new NotSupportedException(); } }
public override int Read(byte[] buffer, int offset, int count) { throw new NotSupportedException(); }
public override long Seek(long offset, System.IO.SeekOrigin direction) { throw new NotSupportedException(); }
public override void SetLength(long length) { throw new NotSupportedException(); }
public override void Close() { _ms.Close(); byte[] buffer_cn = _ms.GetBuffer(); string str_cn = _encoding.GetString( buffer_cn ); //用Strings類的StrConv方法,其中TraditionalChinese是VisualBasic中的一個枚舉 string str_tw = Strings.StrConv(str_cn, VbStrConv.TraditionalChinese, 0); str_tw = str_tw.Replace("__zh-cn__", "__zh-tw__"); byte[] buffer_tw = _encoding.GetBytes(str_tw); using (_sink) { _sink.Write(buffer_tw, 0, buffer_tw.Length); } }
public override void Flush() { _ms.Flush(); }
public override void Write(byte[] buffer, int offset, int count) { _ms.Write(buffer, offset, count); } }
出處:藍色理想
責(zé)任編輯:moby
上一頁 URL重寫實現(xiàn)IHttpHandler接口 下一頁
◎進入論壇網(wǎng)絡(luò)編程版塊參加討論
|