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

推荐订阅源

MyScale Blog
MyScale Blog
Apple Machine Learning Research
Apple Machine Learning Research
H
Help Net Security
雷峰网
雷峰网
V
Visual Studio Blog
G
Google Developers Blog
Microsoft Azure Blog
Microsoft Azure Blog
Hugging Face - Blog
Hugging Face - Blog
爱范儿
爱范儿
IT之家
IT之家
Engineering at Meta
Engineering at Meta
Microsoft Security Blog
Microsoft Security Blog
aimingoo的专栏
aimingoo的专栏
大猫的无限游戏
大猫的无限游戏
M
MIT News - Artificial intelligence
月光博客
月光博客
A
About on SuperTechFans
B
Blog RSS Feed
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
The GitHub Blog
The GitHub Blog
N
Netflix TechBlog - Medium
J
Java Code Geeks
云风的 BLOG
云风的 BLOG
Blog — PlanetScale
Blog — PlanetScale

博客园 - 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();     
  }