怎么样在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
{
内部属性和方法
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
{
成员变量
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
{
内部属性和方法
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
{
成员变量
本文地址:http://www.45fan.com/a/question/69082.html