惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

MongoDB | Blog
MongoDB | Blog
宝玉的分享
宝玉的分享
博客园 - 三生石上(FineUI控件)
小众软件
小众软件
罗磊的独立博客
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
S
SegmentFault 最新的问题
Last Week in AI
Last Week in AI
人人都是产品经理
人人都是产品经理
博客园 - 聂微东
博客园 - 司徒正美
博客园 - 叶小钗
T
Tailwind CSS Blog
博客园 - Franky
V
V2EX
有赞技术团队
有赞技术团队
美团技术团队
雷峰网
雷峰网
爱范儿
爱范儿
Jina AI
Jina AI
D
DataBreaches.Net
H
Help Net Security
酷 壳 – CoolShell
酷 壳 – CoolShell

博客园 - jinchun

Flex XML查找节点 - jinchun - 博客园 常用建站参考网站大本营 ICON图标下载地址汇总 xml 个性主页服务 javascript 拖动 ,并得到变化位置 乱码解决之道 宝玉作品集一 20种让脸美白的简单方法 让孩子超过自己! 正则表达式语法 CSS圆角边框 纯CSS制作 CSS学习网址 韩国网页配色表 网页配色表 http://ajax.cnrui.cn/article/1/5/2006/20061116848.shtml AJAX / JAVASCRIPT 网址收藏 如何实现家财500万 网页设计常用网址
在 ASP.NET 中执行 URL 重写
jinchun · 2007-04-16 · via 博客园 - jinchun

namespace JC.Web
{
 /// <summary>
 /// URLRewriter 的摘要说明。
 /// </summary>
 public class Rewriter : IConfigurationSectionHandler
 {
  protected XmlNode _oRules=null;

  protected Rewriter(){}

  public string GetSubstitution(string zPath)
  {
   Regex oReg;

   foreach(XmlNode oNode in _oRules.SelectNodes("rule"))
   {
    oReg=new Regex(oNode.SelectSingleNode("url/text()").Value);
    Match oMatch=oReg.Match(zPath);

    if(oMatch.Success)
    {
     return oReg.Replace(zPath,oNode.SelectSingleNode("rewrite/text()").Value);
    }
   }

   return zPath;
  }

  public static void Process()
  {
   Rewriter oRewriter=(Rewriter)ConfigurationSettings.GetConfig("system.web/urlrewrites");


   string zSubst=oRewriter.GetSubstitution(HttpContext.Current.Request.Path.ToLower());


   if(zSubst.Length>0)
   {
    HttpContext.Current.RewritePath(zSubst);
   }
  }

  #region Implementation of IConfigurationSectionHandler
  public object Create(object parent, object configContext, XmlNode section)
  {   
   _oRules=section;

   // TODO: Compile all Regular Expressions

   return this;
  }
  #endregion
 }

}

Web.Config

  <urlrewrites>
   
     <rule>
      <url>/default.html</url>
      <rewrite>/default.aspx</rewrite>
     </rule>
     
     <rule>
      <url>/products.html</url>
      <rewrite>products/products.aspx</rewrite>
     </rule>
     
     <rule>
      <url>/axblog.html</url>
      <rewrite>blog/blog.aspx?cid=2</rewrite>
     </rule>
     <rule>
      <url>/blog/a(\d+).html</url>
      <rewrite>/blog/blogdetail.aspx?aid=$1</rewrite>
     </rule>
     <rule>
      <url>/blog/cid(\d+).html</url>
      <rewrite>/blog/blog.aspx?cid=$1</rewrite>
     </rule>
     <rule>
      <url>/blog/c(\d+)a(\d+).html</url>
      <rewrite>/blog/blogdetail.aspx?cid=$1&amp;aid=$2</rewrite>
     </rule>
     <rule>
      <url>/blog/dt(\d+).html</url>
      <rewrite>/blog/blog.aspx?dt=$1</rewrite>
     </rule>
     <rule>
      <url>/support/faq_cid(\d+).html</url>
      <rewrite>/support/faqlist.aspx?cid=$1</rewrite>
     </rule>
      <rule>
      <url>/support/faq_c(\d+)a(\d+).html</url>
      <rewrite>/support/faqdetail.aspx?cid=$1&amp;aid=$2</rewrite>
     </rule>

     <rule>
      <url>(.+)\.html</url>
      <rewrite>$1.aspx</rewrite>
     </rule>    
     
  </urlrewrites>

Global

  protected void Application_BeginRequest(Object sender, EventArgs e)
  {
   JC.Web.Rewriter.Process();     
  }