url重写
一、原理
void Application_BeginRequest(object sender, EventArgs e)
{
var Request = HttpContext.Current.Request;
var Response = HttpContext.Current.Response;
System.Text.RegularExpressions.Regex reg = new Regex(@"/default\-(\d{2})\.aspx",RegexOptions.IgnoreCase);
Match match = reg.Match(Request.RawUrl);
if (match.Success)
{
string s = match.Groups[1].Value;
string url = "default.aspx?id=" + s;
HttpContext.Current.RewritePath(url);
}
}
使用微软写好的 只需进行配置就好
二、urlRewriter
0. 把类库复制到bin下 添加引用
1、在<configSections>节点加入
<section name="RewriterConfig" type="URLRewriter.Config.RewriterConfigSerializerSectionHandler, URLRewriter" />
2、在</configSections>之后加入
<RewriterConfig>
<Rules>
<RewriterRule>
<LookFor>~/(\d{4})/(\d{2})/Default\.aspx</LookFor>
<SendTo>~/Default.aspx?ID=$1</SendTo>
</RewriterRule>
<RewriterRule>
<LookFor>~/(\d{4})/(\d{2})/Default\.html</LookFor>
<SendTo>~/Default.aspx?ID=$2</SendTo> <!--$2 是指的是 (\d{2})-->
</RewriterRule>
</Rules>
</RewriterConfig>
3、<httpHandlers>中加入
<add verb="*" path="*.aspx" type="URLRewriter.RewriterFactoryHandler, URLRewriter" />
<add verb="*" path="*.html" type="URLRewriter.RewriterFactoryHandler, URLRewriter" />