本文為系列首篇,旨在自底向上建立 ASP.NET 請求處理管線完整認知:從 IIS 接收請求、ISAPI 擴展、ASP.NET ISAPI (aspnet_isapi.dll)、HttpRuntime、HttpApplicationFactory、HttpApplication、HttpModule、HttpHandler、Page/Handler 執行,到回應輸出。理解管線每個階段的職責、攔截點、擴展性,是掌握安全機制(驗證/授權/Provider 模型)的基石。
| 副檔名 | ISAPI 擴展 | 說明 |
|---|---|---|
| .asp | asp.dll | 傳統 ASP 解釋器 |
| .aspx | aspnet_isapi.dll | ASP.NET 請求進入點 |
| .asmx | aspnet_isapi.dll | XML Web Service |
| .ashx | aspnet_isapi.dll | HTTP Handler |
| .axd | aspnet_isapi.dll | 內部 HTTP Handler (Trace.axd, WebResource.axd...) |
| .asax | aspnet_isapi.dll | Global.asax 應用程式類別 |
| .ascx | aspnet_isapi.dll | User Control |
HttpRuntime (單例)
│
├── HttpApplicationFactory: 管理 HttpApplication 池
│ │
│ └── HttpApplication (每請求一個實例,池化複用)
│ │
│ ├── HttpContext: 封裝 Request/Response/Session/Cache/User/Items
│ ├── HttpRequest: 輸入資料 (QueryString, Form, Files, Headers, Cookies, Url)
│ ├── HttpResponse: 輸出控制 (Write, Redirect, Headers, Cookies, Filter, Cache)
│ ├── HttpModule 集合: 管線事件攔截 (Auth, Session, Cache, Error, OutputCache...)
│ └── HttpHandler: 終端處理器 (Page, WebService, Generic Handler, MVC Controller)
| 順序 | 事件 | 階段 | 典型用途 |
|---|---|---|---|
| 1 | BeginRequest | 請求開始 | URL Rewrite、記錄存取日誌、啟動計時器 |
| 2 | AuthenticateRequest | 驗證 | FormsAuthenticationModule 驗證 Ticket、WindowsAuthenticationModule 驗證 NTLM/Kerberos |
| 3 | PostAuthenticateRequest | 驗證後 | 自訂 Principal 替換 |
| 4 | AuthorizeRequest | 授權 | UrlAuthorizationModule 檢查 |
| 5 | PostAuthorizeRequest | 授權後 | 權限快取、動態授權邏輯 |
| 6 | ResolveRequestCache | 快取 | OutputCacheModule 檢查輸出快取、略過後續管線 |
| 7 | PostResolveRequestCache | 快取後 | 快取未命中時的預處理 |
| 8 | MapRequestHandler | 處理器對應 | 依路由/副檔名選擇 HttpHandler (Page, .ashx, MVC Controller) |
| 9 | PostMapRequestHandler | 對應後 | Handler 相關初始化 |
| 10 | AcquireRequestState | 狀態獲取 | SessionStateModule 載入 Session、ProfileModule 載入 Profile |
| 11 | PostAcquireRequestState | 狀態後 | Session 可用後的邏輯 |
| 12 | PreRequestHandlerExecute | Handler 前 | 最後準備、效能計數器 |
| 13 | ExecuteRequestHandler | Handler 執行 | Page.ProcessRequest、Controller.Execute、Handler.ProcessRequest |
| 14 | PostRequestHandlerExecute | Handler 後 | 清理 Handler 資源、記錄執行時間 |
| 15 | ReleaseRequestState | 狀態釋放 | SessionStateModule 儲存 Session、更新鎖定 |
| 16 | PostReleaseRequestState | 釋放後 | 狀態持久化完成 |
| 17 | UpdateRequestCache | 快取更新 | OutputCacheModule 儲存回應至快取 |
| 18 | PostUpdateRequestCache | 快取後 | 快取儲存完成 |
| 19 | LogRequest | 記錄 | 自訂審計日誌、健康監控 |
| 20 | PostLogRequest | 記錄後 | |
| 21 | EndRequest | 請求結束 | 壓縮回應 (Gzip/Deflate)、關閉連線、最終清理 |
| 22 | PreSendRequestHeaders / PreSendRequestContent | 發送前 | 最後修改 Headers/Content、移除 Server Header |
system.webServer/modules 取代 system.web/httpModules,runAllManagedModulesForAllRequests 控制靜態檔案是否經 Managed 模組。public class Global : HttpApplication {
void Application_Start() { /* 初始化 DI 容器、快取、排程、靜態資料 */ }
void Application_End() { /* 釋放資源、關閉連線、停止排程 */ }
void Application_Error() { /* 全域例外記錄、友善錯誤頁、通知 */ }
void Session_Start() { /* 初始化 Session 資料、線上人數計數 */ }
void Session_End() { /* 清理 Session 資源、離線處理 */ }
// 以上 22 個管線事件皆可在這裡以 Application_EventName 形式覆寫
}
下一篇將深入剖析 Forms Authentication 機制:Ticket 加密/解密、Cookie 管理、滑動過期、機器金鑰、跨應用程式單一登入 (SSO)、自訂 `MembershipProvider`/`RoleProvider` 實作與 Provider 模型設計精髓。