具體代碼我貼出來 詳細(xì)的解釋我都寫在注釋里了。
代碼如下:
//用到的命名空間
using System;
using System.Diagnostics;
using System.Threading;
using System.Web;
using System.Xml;
using System.Text.RegularExpressions;
using System.IO;
//注意名稱空間
namespace my.Forum
{
//繼承自IHttpModule接口
public class HttpModule : System.Web.IHttpModule
{
/// <summary>
/// 實(shí)現(xiàn)接口的Init方法
/// </summary>
/// <param name="context"></param>
public void Init(HttpApplication context)
{
//建立個(gè)委托讓他執(zhí)行下面的ReUrl_BeginRequest事件
context.BeginRequest += new EventHandler(ReUrl_BeginRequest);
}
/// <summary>
/// 實(shí)現(xiàn)接口的Dispose方法
/// </summary>
public void Dispose()
{
}
private void ReUrl_BeginRequest(object sender, EventArgs e)
{
HttpContext context = ((HttpApplication) sender).Context;
string requestPath = context.Request.Path.ToLower();
//SiteUrls是下面的一個(gè)類,這里大家可以重構(gòu)下
foreach (SiteUrls.URLRewrite url in SiteUrls.GetSiteUrls().Urls)
{
//是否找到了匹配選項(xiàng)
if (Regex.IsMatch(requestPath, url.Pattern, RegexOptions.None | RegexOptions.IgnoreCase))
{
//開始替換成我們程序能讀懂的url
string newUrl = Regex.Replace(requestPath.Substring(context.Request.Path.LastIndexOf("/")),
url.Pattern, url.QueryString, RegexOptions.None | RegexOptions.IgnoreCase);
//這里你可以輸出一下看看
//context.Response.Write(url.Page + "<br>" + newUrl+"<br>");
//開始把用戶便于記憶的URL替換成程序能讀懂的url
context.RewritePath(url.Page, string.Empty, newUrl);
}
}
}
}
public class SiteUrls
{
//定義成volatitle類型主要是為了多線程訪問方便,在這個(gè)示例程序中沒什么實(shí)際意義,項(xiàng)目大了就有用了
private static volatile SiteUrls instance = null;
string UrlsFile = HttpContext.Current.Server.MapPath("/config/urls.config");
//定義兩個(gè)屬性
private System.Collections.ArrayList _Urls;
public System.Collections.ArrayList Urls
{
get { return _Urls; }
set { _Urls = value; }
}
//這個(gè)就是個(gè)鍵植對 表害怕一點(diǎn)也不高深
private System.Collections.Specialized.NameValueCollection _Paths;
public System.Collections.Specialized.NameValueCollection Paths
{
get { return _Paths; }
set { _Paths = value; }
}
//構(gòu)造函數(shù)
private SiteUrls()
{
Urls = new System.Collections.ArrayList();
Paths = new System.Collections.Specialized.NameValueCollection();
//以XML個(gè)數(shù)讀取那CONFIG文件
XmlDocument urlconfig = new XmlDocument();
urlconfig.Load(UrlsFile);
XmlNode root = urlconfig.SelectSingleNode("urls");
foreach (XmlNode n in root.ChildNodes)
{
//XmlNodeType.Comment如果不是注釋
if (n.NodeType != XmlNodeType.Comment && n.Name.ToLower() == "rewrite")
{
XmlAttribute name = n.Attributes["name"];
XmlAttribute path = n.Attributes["path"];
XmlAttribute page = n.Attributes["page"];
XmlAttribute querystring = n.Attributes["querystring"];
XmlAttribute pattern = n.Attributes["pattern"];
if (name != null && path != null && page != null
&& querystring != null && pattern != null)
{
Paths.Add(name.Value, path.Value);
//壓進(jìn)去的都是url實(shí)體類
Urls.Add(new URLRewrite(name.Value, pattern.Value, page.Value.Replace("^", "&"),
querystring.Value.Replace("^", "&")));
}
}
}
}
//用方法實(shí)例化
public static SiteUrls GetSiteUrls()
{
if (instance == null)
{
instance = new SiteUrls();
}
return instance;
}
#region url實(shí)體類
public class URLRewrite
{
#region 成員變量
private string _Name;
public string Name
{
get { return _Name; }
set { _Name = value; }
}
private string _Pattern;
public string Pattern
{
get { return _Pattern; }
set { _Pattern = value; }
}
private string _Page;
public string Page
{
get { return _Page; }
set { _Page = value; }
}
private string _QueryString;
public string QueryString
{
get { return _QueryString; }
set { _QueryString = value; }
}
#endregion
#region 構(gòu)造函數(shù)
public URLRewrite(string name, string pattern, string page, string querystring)
{
_Name = name;
_Pattern = pattern;
_Page = page;
_QueryString = querystring;
}
#endregion
}
#endregion
}
}
聲明:本網(wǎng)頁內(nèi)容旨在傳播知識(shí),若有侵權(quán)等問題請及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com