ASP.NET Web Form 中請求直接對應實體檔案,這種緊密綁定帶來靈活性、可讀性與 SEO 三大限制。URL 路由系統透過動態映射機制,實現請求網址與實體路徑的分離。
IIS URL 重寫模組在原生代碼層面運作,適用所有 IIS 託管應用;ASP.NET 路由則以託管代碼實現,屬 ASP.NET 管線一部分。
建立員工列表頁(/employees)與員工詳情頁(/employees/{姓名}/{ID}),後者 URL 同時包含可讀姓名與唯一 ID。員工資料定義於 Employee 類別,透過 EmployeeRepository 提供資料存取。
public class Employee
{
public string Id { get; private set; }
public string Name { get; private set; }
public string Gender { get; private set; }
public DateTime BirthDate { get; private set; }
public string Department { get; private set; }
// 建構函式略
}
public class EmployeeRepository
{
private static IList<Employee> employees;
static EmployeeRepository() { /* 初始化測試資料 */ }
public IEnumerable<Employee> GetEmployees(string id = "") { /* 依 ID 篩選 */ }
}
