手工在 Global.asax 寫重寫邏輯已過時,成熟組件(如微軟提供的 URLRewriter)更便捷。
web.config 註冊 HttpModule:<httpModules>
<add name="ModuleRewriter" type="URLRewriter.ModuleRewriter, URLRewriter" />
</httpModules>
configPath 外部化):<configSections>
<section name="RewriterConfig" type="URLRewriter.Config.RewriterConfigSerializerSectionHandler, URLRewriter" />
</configSections>
<RewriterConfig>
<Rules>
<RewriterRule>
<LookFor>~/tag/([\w]+)/</LookFor>
<SendTo>~/Tags.aspx?Tag=$1</SendTo>
</RewriterRule>
</Rules>
</RewriterConfig>
模組掛載於 BeginRequest,依正則捕獲群組($1)重組目標 URL,後續管線視為直接請求目標頁面。程式碼可透過 Request.QueryString["Tag"] 取值。
後續文章將介紹更輕量的替代方案。