tags: MVC

MVC Routing

Route

路由定義 URL 模式和處理應用程序資訊。應用程序的所有已配置路由都存在 RouteTable 中,當請求傳入時,路由引擎將使用它來判斷合適的處理程序類別或檔案。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Configure a Route

App_Start\RouteConfig.cs

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

routes.IgnoreRoute

用來定義不要透過 Routing 處理的網址,例如: 不會處理網址 localhost/test.axd/abc/123。
(* 表示後面無論接幾個路徑,如:/xxx/xxx 或/xxx/xxx/xxx 皆不處理)

routes.MapRoute

  • name: Route 的名稱,必須是唯一的。
  • url: 具名參數定義 URL 樣式與每個路徑段落(PathSegment)的 Route Value 參數名稱。
  • defaults: 具名參數定義各 RouteValue 路由參數的預設值,當網址路由比對不到 HTTP 要求的網址時,會先嘗試帶入這裡定義的預設值,然後再進一步比對是否有符合的 Controller 和 Action 可以執行。

id = UrlParameter.Optional,id 路由參數為選擇性,如果沒有為空值。

//RouteConfig.cs public class RouteConfig { public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute( name: "Default", url: "{controller}/{action}/{id}", defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } ); } }

URL Pattern

  • {controller}/{action}/{id}表示定義的 URL 樣式包含三個路由參數,分別命名為 controller、action 與 id。
  • 網址路徑只有 localhost,那預設的路由為 localhost/home/index
  • 網址路徑為 /Home/About/123,那 controller 的路由值是 Home,action 的路由值是 About,而 id 這個路由值就是 123。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Multiple Routes

  • 路由順序由上到下匹配。
  • 自訂路由 url: customer/who/{id}
  • 沒有加id = UrlParameter.Optional,表示網址必須要有路由參數 id。
  • 網址 http://localhost/customer/who/100,路由比對為 Customer Controller 的 Action Who
//RouteConfig.cs public class RouteConfig { public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); //自訂路由 routes.MapRoute( name: "Customer", url: "customer/who/{id}", defaults: new { controller = "Customer", action = "Who"} ); //預設路由 routes.MapRoute( name: "Default", url: "{controller}/{action}/{id}", defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } ); } }

Route Constraints

限制路由參數 id 只能是數字。

//RouteConfig.cs public class RouteConfig { public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute( name: "Customer", url: "customer/who/{id}", defaults: new { controller = "Customer", action = "Index"}, constraints: new { id = @"\d+" } //限制 id 路由參數只能是數字 ); routes.MapRoute( name: "Default", url: "{controller}/{action}/{id}", defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } ); } }

Register Routes

要在 Global.asax.cs 的 Application_Start 註冊 RouteConfig.cs,將所有路由包含到 RouteTable 中。

//Global.asax.cs public class MvcApplication : System.Web.HttpApplication { protected void Application_Start() { RouteConfig.RegisterRoutes(RouteTable.Routes); } }

參考資料 ASP.NET MVC Tutorials