怎么样在ASP.NET2.0中实现URL重写?
本文参考了网上已有代码,在此基础上进行了整理归纳,总结出在ASP.NET2.0环境下实现URL重写的行之有效的方法。如果转载,请注明出处:雪尘的专栏
一、在网站中添加MyHttpModule类,代码如下:
usingSystem;
usingSystem.Collections;
usingSystem.Collections.Specialized;
usingSystem.IO;
usingSystem.Text;
usingSystem.Text.RegularExpressions;
usingSystem.Xml;
usingSystem.Web;
usingSystem.Web.UI;
usingSystem.Web.Caching;
namespaceUrlRewrite

...{
publicclassMyHttpModule:IHttpModule

...{
publicvoidInit(HttpApplicationapp)

...{
app.AuthorizeRequest+=newEventHandler(app_AuthorizeRequest);
}

publicvoidDispose()...{}
protectedvoidRewrite(stringrequestedPath,System.Web.HttpApplicationapp)

...{
//app.Context.RewritePath("~/default.aspx",string.Empty,"test=tttttttt");
foreach(URLRewriteurlinSiteUrls.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");
}
}
privatevoidapp_AuthorizeRequest(objectsender,EventArgse)

...{
HttpApplicationapp=(HttpApplication)sender;
Rewrite(app.Request.Path,app);
}
}
publicclassSiteUrls

...{

内部属性和方法#region内部属性和方法
stringSiteUrlsFile=HttpContext.Current.Server.MapPath(System.Configuration.ConfigurationManager.AppSettings["SiteUrls"]);
privateArrayList_Urls;
publicArrayListUrls

...{
get

...{
return_Urls;
}
set

...{
_Urls=value;
}
}
privateNameValueCollection_Paths;
publicNameValueCollectionPaths

...{
get

...{
return_Paths;
}
set

...{
_Paths=value;
}
}
privateSiteUrls()

...{
stringapplicationPath=HttpContext.Current.Request.ApplicationPath;
if(applicationPath=="/")

...{
applicationPath=string.Empty;
}
Urls=newArrayList();
Paths=newNameValueCollection();
Paths.Add("home",applicationPath);
XmlDocumentxml=newXmlDocument();
xml.Load(SiteUrlsFile);
XmlNoderoot=xml.SelectSingleNode("SiteUrls");
foreach(XmlNodeninroot.ChildNodes)

...{
if(n.NodeType!=XmlNodeType.Comment&&n.Name.ToLower()=="rewrite")

...{
XmlAttributename=n.Attributes["name"];
XmlAttributepath=n.Attributes["path"];
XmlAttributepage=n.Attributes["page"];
XmlAttributequerystring=n.Attributes["querystring"];
XmlAttributepattern=n.Attributes["pattern"];
if(name!=null&&path!=null&&page!=null&&querystring!=null&&pattern!=null)

...{
Paths.Add(name.Value,applicationPath+path.Value);
Urls.Add(newURLRewrite(name.Value,Paths["home"]+pattern.Value,Paths["home"]+page.Value.Replace("^","&"),querystring.Value.Replace("^","&")));
}
}
}
}
#endregion
publicstaticSiteUrlsGetSiteUrls()

...{
stringCacheKey="SiteUrls";
SiteUrlsurls=System.Web.HttpContext.Current.Cache["SiteUrls"]asSiteUrls;
if(urls==null)

...{
urls=newSiteUrls();
System.Web.HttpContext.Current.Cache.Insert(CacheKey,urls,newCacheDependency(urls.SiteUrlsFile),DateTime.MaxValue,TimeSpan.Zero,CacheItemPriority.High,null);
}
returnurls;
}

/**////<summary>
///输出URL示例
///</summary>
///<paramname="id"></param>
///<returns></returns>
publicstringShow(intid)

...{
returnstring.Format(Paths["Show"],id);
}
}
publicclassURLRewrite

...{

成员变量#region成员变量
privatestring_Name;
publicstringName

...{
get

...{
return_Name;
}
set

...{
_Name=value;
}
}
privatestring_Pattern;
publicstringPattern

...{
get

...{
return_Pattern;
本文地址:
http://www.45fan.com/a/question/69082.html