将之前的<<在ASP.NET中重写URL>>中的代码重构一下:
Global.asax.cs文体中:
补充:ASP.NET 2.0的URL映射的实现方法
在Web.config->configuration->system.web部分实现:
将首页Default.aspx映射为Index.html,将InformationDissemination.aspx映射为InformationDissemination.html
Global.asax.cs文体中:
protected void Application_BeginRequest(object sender, EventArgs e)
{
string pattern = @"(ArticleContent)-(\d+).html";
MyRewritePath(pattern, "$1.aspx?id=$2");
//将ArticleContent.aspx?id=n改写为ArticleContent-n.html的形式(n为参数)
string pattern1 = @"(ArticleList)-(\d+).html";
MyRewritePath(pattern1, "$1.aspx?caid=$2");
//将ArticleList.aspx?caid=n改写为ArticleList-n.html的形式
string pattern2 = @"(SsStorieDetailed)-(\d+).html";
MyRewritePath(pattern2, "$1.aspx?id=$2");
//将SsStorieDetailed.aspx?id=n改写为SsStorieDetailed-n.html的形式
}
protected void MyRewritePath(string pattern,string regex)
{
string oldUrl = HttpContext.Current.Request.RawUrl;
Match m = Regex.Match(oldUrl, pattern, RegexOptions.IgnoreCase | RegexOptions.Compiled);
if (m.Success)
{
string newUrl = Regex.Replace(oldUrl, pattern, regex, RegexOptions.Compiled | RegexOptions.IgnoreCase);
this.Context.RewritePath(newUrl);
}
}
补充:ASP.NET 2.0的URL映射的实现方法
在Web.config->configuration->system.web部分实现:
<urlMappings enabled="true">
<add url="~/Index.html" mappedUrl="~/Default.aspx" />
<add url="~/InformationDissemination.html" mappedUrl="~/InformationDissemination.aspx" />
</urlMappings>
将首页Default.aspx映射为Index.html,将InformationDissemination.aspx映射为InformationDissemination.html