若想在 ASP.NET 專案中使用純 HTML 頁面替代 ASPX,核心挑戰在於如何將前端請求路由到後端邏輯。傳統 .ashx 一般處理程序雖可行,但每個 Action 需對應一個處理程序,維護成本高。更優雅的方案是利用 表單提交參數區分 Action。
<form action="/api/Handler.ashx" method="post">
<input type="hidden" name="action" value="login" />
<input type="text" name="username" />
<input type="password" name="password" />
<button type="submit">登入</button>
</form>
後端 Handler.ashx 中:
public void ProcessRequest(HttpContext context) {
string action = context.Request["action"];
switch (action) {
case "login": DoLogin(context); break;
case "register": DoRegister(context); break;
// ...
}
}
選擇哪種取決於專案規模與團隊技術棧,核心原則是:單一入口、參數路由、邏輯集中。