【url重写】

一、原理
void Application_BeginRequest(object sender, EventArgs e)
    {
  //url重写
        HttpApplication app = sender as HttpApplication;
        string url = app.Request.RawUrl;
        Regex r = new Regex("/(\d+)/details\.htm",RegexOptions.IgnoreCase);
        Match m = r.Match(url);
        if (m.Success)
        {
            string id = m.Groups[1].Value;
            app.Context.RewritePath("~/PhotoDetails.aspx?id=" + id);
        }
    }

 二、urlRewriter
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>
    </Rules>
  </RewriterConfig>
3、<httpHandlers>中加入
<add verb="*" path="*.aspx" type="URLRewriter.RewriterFactoryHandler, URLRewriter" />
或者 <httpModules>加入
<add type="URLRewriter.ModuleRewriter, URLRewriter" name="ModuleRewriter" />

备注:关于url组件可在文件中下载

原文地址:https://www.cnblogs.com/baiboy/p/3150726.html