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

推荐订阅源

Engineering at Meta
Engineering at Meta
博客园_首页
H
Help Net Security
WordPress大学
WordPress大学
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
罗磊的独立博客
博客园 - 三生石上(FineUI控件)
B
Blog
I
InfoQ
SecWiki News
SecWiki News
T
Tailwind CSS Blog
Spread Privacy
Spread Privacy
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
V
Vulnerabilities – Threatpost
N
Netflix TechBlog - Medium
P
Palo Alto Networks Blog
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Vercel News
Vercel News
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
K
Kaspersky official blog
M
MIT News - Artificial intelligence
S
Schneier on Security
T
Threat Research - Cisco Blogs
F
Fortinet All Blogs
Cyberwarzone
Cyberwarzone
Scott Helme
Scott Helme
aimingoo的专栏
aimingoo的专栏
Martin Fowler
Martin Fowler
MyScale Blog
MyScale Blog
The Cloudflare Blog
Recent Announcements
Recent Announcements
Security Latest
Security Latest
G
GRAHAM CLULEY
IT之家
IT之家
Y
Y Combinator Blog
The Last Watchdog
The Last Watchdog
腾讯CDC
Google DeepMind News
Google DeepMind News
V
V2EX
S
Securelist
TaoSecurity Blog
TaoSecurity Blog
B
Blog RSS Feed
S
SegmentFault 最新的问题
博客园 - 叶小钗
P
Proofpoint News Feed
云风的 BLOG
云风的 BLOG
Project Zero
Project Zero
G
Google Developers Blog
Google DeepMind News
Google DeepMind News
F
Full Disclosure

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