系列最終篇探討 IIS 層級(ISAPI/C++)與 ASP.NET 層級(託管代碼)URL Rewrite 的差異、選型考量與進階組合技巧。
理論上 IIS 層級(原生碼)效能優於 ASP.NET 層級,但實務差距極小,幾乎不可能成為瓶頸。選型不應以效能為首要依據。
/articles → /ArticleList.aspx,再在 Global.asax Application_BeginRequest 依 Request.Url.Host 追加 ?owner=xxx。
// IIRF 規則
RewriteRule ^/Articles$ /ArticleList.aspx [I, L, U]
// Global.asax 第二次重寫
protected void Application_BeginRequest(object sender, EventArgs e) {
var ctx = HttpContext.Current;
string host = ctx.Request.Url.Host;
string owner = host.Substring(0, host.IndexOf('.'));
ctx.RewritePath(ctx.Request.RawUrl + "?owner=" + owner);
}
實務上建議將第二層邏輯移至獨立 HttpModule,並加強 QueryString 判斷與安全驗證。