.net 中的伪静态方式. 来自 Discuz!NT
<httpModules>
<add type="Discuz.Forum.HttpModule, Discuz.Forum" name="HttpModule" />
</httpModules>
<?xml version="1.0" encoding="utf-8" ?>
<urls>
<rewrite name="showforum"
path="/showforum-{0}-{1}.aspx"
pattern = "/showforum-(\d+)(-(\d+))?.aspx"
page="/showforum.aspx"
querystring="forumid=$1^page=$3" />
<rewrite name="showtopic"
path="/showtopic-{0}-{1}.aspx"
pattern = "/showtopic-(\d+)(-(\d+))?.aspx"
page="/showtopic.aspx"
querystring="topicid=$1^page=$3" />
<rewrite name="userinfo"
path="/userinfo-{0}.aspx"
pattern = "/userinfo-(\d+)*.aspx"
page="/userinfo.aspx"
querystring="userid=$1" />
<rewrite name="rss"
path="/rss-{0}.aspx"
pattern = "/rss(-(\d+))?.aspx"
page="/rss.aspx"
querystring="forumid=$2" />
</urls>
![]() /**//*
* Discuz!NT Version: 1.0
* Created on 2007-3-30
*
* Web: http://www.discuznt.com
* Copyright (C) 2001 - 2007 Comsenz Technology Inc., All Rights Reserved.
* This is NOT a freeware, use is subject to license terms.
*
*/
![]()
![]()
using System;
using System.Diagnostics;
using System.Threading;
using System.Web;
using System.Xml;
using System.Text.RegularExpressions;
using Discuz.Common;
![]()
namespace Discuz.Forum
![]() ![]() {
![]() /**//// <summary>
/// HttpModule 的摘要说明。
/// </summary>
public class HttpModule : System.Web.IHttpModule
![]() {
//public readonly static Mutex m=new Mutex();
![]()
![]() /**//// <summary>
/// 实现接口的Init方法
/// </summary>
/// <param name="context"></param>
public void Init(HttpApplication context)
![]() {
OnlineUserFactory.ResetOnlineList();
context.BeginRequest += new EventHandler(ReUrl_BeginRequest);
![]()
}
![]()
public void Application_OnError(Object sender , EventArgs e)
![]() {
HttpApplication application = (HttpApplication)sender;
HttpContext context = application.Context;
//if (context.Server.GetLastError().GetBaseException() is MyException)
![]() {
//MyException ex = (MyException) context.Server.GetLastError().GetBaseException();
context.Response.Write("<html><body style=\"font-size:14px;\">");
context.Response.Write("Discuz!NT Error:<br />");
context.Response.Write("<textarea name=\"errormessage\" style=\"width:80%; height:200px; word-break:break-all\">");
context.Response.Write(System.Web.HttpUtility.HtmlEncode(context.Server.GetLastError().ToString()));
context.Response.Write("</textarea>");
context.Response.Write("</body></html>");
context.Response.End();
}
![]()
}
![]()
![]() /**//// <summary>
/// 实现接口的Dispose方法
/// </summary>
public void Dispose()
![]() {
}
![]()
![]() /**//// <summary>
/// 重写Url
/// </summary>
/// <param name="sender">事件的源</param>
/// <param name="e">包含事件数据的 EventArgs</param>
private void ReUrl_BeginRequest(object sender, EventArgs e)
![]() {
BaseConfigInfo config = Providers.BaseConfigProvider.Instance();
if (config == null)
return;
HttpContext context = ((HttpApplication)sender).Context;
string forumPath = BaseConfigFactory.GetForumPath.ToLower();
![]()
string requestPath = context.Request.Path.ToLower();
![]()
if (requestPath.StartsWith(forumPath))
![]() {
if (requestPath.Substring(forumPath.Length).IndexOf("/") == -1)
![]() {
// 当前样式id
string strTemplateid = ConfigFactory.GetDefaultTemplateID().ToString();
//string ttt = Utils.GetCookie("dnttemplateid");
if (Utils.InArray(Utils.GetCookie("dnttemplateid"), TemplateFactory.GetValidTemplateIDList()))
![]() {
strTemplateid = Utils.GetCookie("dnttemplateid");
}
![]()
foreach (SiteUrls.URLRewrite url in SiteUrls.GetSiteUrls().Urls)
![]() {
if (Regex.IsMatch(requestPath, url.Pattern, RegexOptions.Compiled|RegexOptions.IgnoreCase))
![]() {
string newUrl = Regex.Replace(requestPath.Substring(context.Request.Path.LastIndexOf("/")), url.Pattern, url.QueryString, RegexOptions.Compiled|RegexOptions.IgnoreCase);
context.RewritePath(forumPath + "aspx/" + strTemplateid + url.Page, string.Empty, newUrl);
![]()
return;
}
}
context.RewritePath(forumPath + "aspx/" + strTemplateid + requestPath.Substring(context.Request.Path.LastIndexOf("/")));
}
else if (requestPath.StartsWith(forumPath + "archiver/"))
![]() {
string path = requestPath.Substring(forumPath.Length + 8);
foreach(SiteUrls.URLRewrite url in SiteUrls.GetSiteUrls().Urls)
![]() {
if (Regex.IsMatch(path, url.Pattern, RegexOptions.Compiled|RegexOptions.IgnoreCase))
![]() {
string newUrl = Regex.Replace(path, url.Pattern, url.QueryString, RegexOptions.Compiled|RegexOptions.IgnoreCase);
context.RewritePath(forumPath + "archiver" + url.Page, string.Empty, newUrl);
return;
}
}
return;
}
else if (requestPath.StartsWith(forumPath + "tools/"))
![]() {
string path = requestPath.Substring(forumPath.Length + 5);
foreach (SiteUrls.URLRewrite url in SiteUrls.GetSiteUrls().Urls)
![]() {
if (Regex.IsMatch(path, url.Pattern, RegexOptions.Compiled|RegexOptions.IgnoreCase))
![]() {
string newUrl = Regex.Replace(path, url.Pattern, url.QueryString, RegexOptions.Compiled|RegexOptions.IgnoreCase);
context.RewritePath(forumPath + "tools" + url.Page, string.Empty, newUrl);
return;
}
}
return;
}
![]()
}
![]()
}
![]()
}
![]()
![]()
![]()
![]() /**///////////////////////////////////////////////////////////////////////
public class SiteUrls
![]() {
![]() 内部属性和方法#region 内部属性和方法
private static object lockHelper = new object();
private static volatile SiteUrls instance = null;
![]()
string SiteUrlsFile = HttpContext.Current.Server.MapPath(BaseConfigFactory.GetForumPath + "config/urls.config");
private System.Collections.ArrayList _Urls;
public System.Collections.ArrayList Urls
![]() {
get
![]() {
return _Urls;
}
set
![]() {
_Urls = value;
}
}
![]()
private System.Collections.Specialized.NameValueCollection _Paths;
public System.Collections.Specialized.NameValueCollection Paths
![]() {
get
![]() {
return _Paths;
}
set
![]() {
_Paths = value;
}
}
private SiteUrls()
![]() {
Urls = new System.Collections.ArrayList();
Paths = new System.Collections.Specialized.NameValueCollection();
![]()
XmlDocument xml = new XmlDocument();
![]()
xml.Load(SiteUrlsFile);
![]()
XmlNode root = xml.SelectSingleNode("urls");
foreach(XmlNode n in root.ChildNodes)
![]() {
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);
Urls.Add(new URLRewrite(name.Value, pattern.Value, page.Value.Replace("^", "&"), querystring.Value.Replace("^", "&")));
}
}
}
}
#endregion
![]()
public static SiteUrls GetSiteUrls()
![]() {
if (instance == null)
![]() {
lock (lockHelper)
![]() {
if (instance == null)
![]() {
instance = new SiteUrls();
}
}
}
return instance;
![]()
}
![]()
public static void SetInstance(SiteUrls anInstance)
![]() {
if (anInstance != null)
instance = anInstance;
}
![]()
public static void SetInstance()
![]() {
SetInstance(new SiteUrls());
}
![]()
![]() /**//// <summary>
/// 输出URL示例
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public string Show(int id)
![]() {
return string.Format(Paths["Show"], id);
}
![]()
![]()
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 构造函数
public URLRewrite(string name, string pattern, string page, string querystring)
![]() {
_Name = name;
_Pattern = pattern;
_Page = page;
_QueryString = querystring;
}
#endregion
}
![]()
}
![]()
![]()
![]()
![]()
![]()
![]()
![]()
}
![]()
|