知其然更要知其所以然。掌握 IIS、ASP.NET Runtime、HttpApplication、Module、Handler 協作細節,能精準排查效能瓶頸、安全漏洞、生命週期事件順序錯誤等疑難雜症。
.aspx → aspnet_isapi.dll (IIS 6/7 經典模式)system.webServer/handlers,無 ISAPI 跳板關鍵設定:「確認檔案是否存在」 核取方塊控制 URL Rewrite/虛擬路徑是否觸發 404。
HttpWorkerRequestAppDomain 中載入 HttpRuntimeHttpRuntime.ProcessRequest(HttpWorkerRequest)HttpRuntime 擁有請求的所有資訊:URL、QueryString、Headers、ServerVariables![]()
HttpContext (含 Request、Response、Session、User、Cache、Items)IHttpModule (如 FormsAuthenticationModule, SessionStateModule, UrlRoutingModule)Response.End())IHttpHandler 處理實際邏輯Page 實作 IHttpHandler,.aspx 頁面即 HandlerMvcHandler 處理路由後的 Controller 執行EndRequest 事件),執行清理、壓縮、記錄日誌對應管道「去程/返程」:
Executing (Handler 前) → Executed (Handler 後)
Inserting (資料源操作前) → Inserted (操作後)
HttpContext (Current)
├─ HttpRequest (輸入)
├─ HttpResponse (輸出)
├─ HttpSessionState (Session)
├─ HttpApplicationState (Application)
├─ IPrincipal User (身份)
└─ IDictionary Items (請求級資料共享)
HttpApplication (每請求一實例,池化複用)
├─ Events: BeginRequest, AuthenticateRequest, AuthorizeRequest, ...
├─ Modules 集合 (IHttpModule)
└─ Handler (IHttpHandler) → Page / MvcHandler / Ashx / 自訂
IHttpHandler
└─ ProcessRequest(HttpContext) → 單一入口點
Global.asax 事件順序即管道順序,可注入日誌追蹤IHttpModule 記錄請求耗時、參數、例外HttpContext.Current.Items 跨模組/Handler 傳遞資料 (請求生命週期)ASP.NET Core 移除 HttpModule/Handler,改以 Middleware Pipeline 取代:
app.Use(async (context, next) => {
// Before next (相當於 Module 去程)
await next();
// After next (相當於 Module 返程)
});
app.Map("/api", api => api.UseMiddleware()); // 相當於 Handler 映射
概念一脈相承:管道、中介軟體、終端處理器。