相反,在構建ASP.NET MVC應用程式時,URL和頁面就不是一一對應關係了,在ASP.NET MVC應用程式中,一個URL對應一個控制器行為,而不是硬碟上的一個分頁檔。
在傳統ASP.NET和ASP應用程式中,瀏覽器請求被映射到頁面,在ASP.NET MVC應用程式中,瀏覽器請求映射到控制器行為,ASP.NET應用程式是以內容為中心,相反,ASP.NET MVC應用程式是以應用邏輯為中心。
理解URL路由
瀏覽器請求通過ASP.NET MVC的一個叫做URL路由的特性映射到控制器行為,URL路由路由入站請求給控制器行為。
URL路由使用路由表處理入站請求,當你的應用程式第一次啟動時建立這個路由表,路由表配置在Global.asax檔案中,預設的MVC Global.asax檔案內容如清單1所示。
清單1 Global.asax
Public Class GlobalApplication
Inherits System.Web.HttpApplication
Shared Sub RegisterRoutes(ByVal routes As RouteCollection)
routes.IgnoreRoute("{resource}.axd/{*pathInfo}")
' MapRoute takes the following parameters, in order:
' (1) Route name
' (2) URL with parameters
' (3) Parameter defaults
routes.MapRoute( _
"Default", _
"{controller}/{action}/{id}", _
New With {.controller = "Home", .action = "Index", .id = ""} _
)
End Sub
Sub Application_Start()
RegisterRoutes(RouteTable.Routes)
End Sub
End Class