| 特性 | .ashx (Generic Handler) | .axd (Custom Handler) |
|---|---|---|
| 註冊方式 | 專案中「新增項目」→ Generic Handler,自動註冊 | 需在 web.config <httpHandlers> 手動註冊 |
| 程式碼位置 | 專案內 .ashx.cs | 獨立組件 (DLL),不在 Web 專案中 |
| 適用情境 | 專案內部處理:圖片產生、檔案下載、AJAX 端點 | 跨專案共用、第三方控制項、元件庫輸出資源 |
| URL 形式 | /Handler.ashx | /Resource.axd |
<configuration>
<system.web>
<httpHandlers>
<add verb="*" path="OpenSearch.axd"
type="Company.Components.HttpHandler.OpenSearchHandler, Company.Extensions"
validate="false" />
</httpHandlers>
</system.web>
<system.webServer> <!-- IIS 7+ Integrated Mode -->
<handlers>
<add name="OpenSearch" verb="*" path="OpenSearch.axd"
type="Company.Components.HttpHandler.OpenSearchHandler, Company.Extensions" />
</handlers>
</system.webServer>
</configuration>
type 格式:完整類別名稱, 組件名稱(不含 .dll)。<system.webServer><handlers> 重複註冊。當處理常式程式碼編譯在獨立 DLL中(如商業控制項庫、共用元件),Web 專案無法直接參考 .ashx,必須透過 httpHandlers 將特定路徑映射到該 DLL 中的 IHttpHandler 實作類別。
.axd → aspnet_isapi.dll 對應,導致 404/403。需要求主機商開啟對應。validate="false" 可避免啟動時載入組件失敗拋出例外(僅在真正請求時載入)。httpHandlers 註冊 path="Something.ashx",純屬慣例。專案內自用 → .ashx;跨專案/元件庫輸出 → .axd + web.config 註冊。