頁面引用數十個 JS/CSS 怎麼自動合併且無需人工干預?結合 HttpModule (攔截回應) 與 HttpHandler (處理合併請求) 實現透明合併壓縮。
將所有 <script>、<link> 替換為 <resource runat="server">:
<head runat="server">
<resource src='https://cdn.example.com/js/apiCommon.js' type="text/javascript" runat="server"></resource>
<resource href='https://cdn.example.com/css/site.css' type="text/css" rel="stylesheet" runat="server" />
</head>
使用者控制項同理。
Init 註冊 PostRequestHandlerExecute 事件。Content-Type == text/html。HtmlAgilityPack 或 Regex 解析 <resource> 標籤,收集 src/href、type。/combined.axd?type=js&files=file1.js,file2.js。<resource> 為單一 <script>/<link> 指向合併 URL。combined.axd 對應 CombinedResourceHandler : IHttpHandler。ProcessRequest:解析查詢字串,讀取各檔案內容 (支援 CDN 快取)、依序拼接、設定 Content-Type、輸出。ETag、Last-Modified、Cache-Control: public, max-age=31536000。HttpModule 負責「頁面級重寫」,HttpHandler 負責「資源級合併輸出」,兩者分工協作,構建透明的前端資源優化管線。