日期:2010-08-04 浏览次数:20389 次
需要源代码请在评论里留下email
关键代码:
public class MyHttpModule : IHttpModule
{
public void Init(HttpApplication app)
{
app.AuthorizeRequest += new EventHandler(app_AuthorizeRequest);
}
public void Dispose() {}
protected void Rewrite(string requestedPath, System.Web.HttpApplication app)
{
// app.Context.RewritePath("~/default.aspx", string.Empty, "test=tttttttt");
foreach(URLRewrite url in SiteUrls.GetSiteUrls().Urls)
{
if (Regex.IsMatch(app.Context.Request.Path, url.Pattern, RegexOptions.Compiled|RegexOptions.IgnoreCase))
{
app.Context.RewritePath(url.Page, string.Empty, Regex.Replace(app.Context.Request.Path, url.Pattern, url.QueryString, RegexOptions.Compiled|RegexOptions.IgnoreCase));
return;
}
}
if (app.Context.Request.Path.ToLower().EndsWith(".shtml"))
{
app.Context.Response.Redirect("~/index.html");
}
}
private void app_AuthorizeRequest(object sender, EventArgs e)
{
HttpApplication app = (HttpApplication) sender;
Rewrite(app.Request.Path, app);
}
}
public class SiteUrls
{
#region 内部属性和方法
string SiteUrlsFile = HttpContext.Current.Server.MapPath(ConfigurationSettings.AppSettings["SiteUrls"]);
private ArrayList _Urls;
public ArrayList Urls
{
get
{
return _Urls;
}
set
{
_Urls = value;
}
}
private NameValueCollection _Paths;
public NameValueCollection Paths
{
get
{
return _Paths;
}
set
{
_Paths = value;
}
}
private SiteUrls()
{
string applicationPath = HttpContext.Current.Request.ApplicationPath;
if (applicationPath == "/")
{
applicationPath = string.Empty;
}
Urls = new ArrayList();
Paths = new NameValueCollection();
Paths.Add("home", applicationPath);
XmlDocument xml = new XmlDocument();
xml.Load(SiteUrlsFile);
XmlNode root = xml.SelectSingleNode("SiteUrls");
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)
{