URL Routing with MVC
Introduction
The ASP.NET MVC framework uses URL routing to map URLs to controller classes and actions. URL routing parses variables in the URL according to a pattern that you define, and automatically passes the variables to a controller action as parameter arguments.
This topic describes how to use URL routing with MVC. For general information about how to define routes and construct URLs dynamically, see URL Routing.
Default Routes
ASP.NET MVC project templates include preconfigured URL routes that are defined in the ASP.NET Application class in the Global.asax file. These routes enable you to get started on an ASP.NET MVC application without having to explicitly configure routes for the most common scenarios.
The following table lists the default URL patterns.
|
Default URL Patterns
|
Examples of matching URL
|
|
{controller}/{action}/{id}
|
http://server/application/Products/show/beverages
|
|
Default.aspx
|
http://server/application/Default.aspx
|
These routes are defined with default values that are used when a request is processed. The default values for each route specify which controller, action, and ID to use if those values are not provided in the URL. The default values for each pattern are shown in the following table.
|
Default URL Patterns
|
Default values
|
|
{controller}/{action}/{id}
|
action = "Index"
id = null (Nothing in Visual Basic)
|
|
Default.aspx
|
controller = "Home"
action = "Index"
id = null (Nothing in Visual Basic)
|
When processing a request for the Default.aspx page, the MVC framework will always use the default values for that route because there are no URL parameters to retrieve. When processing a request for /Products/show, the MVC framework will assign "Products" to the controller parameter, "show" to the action parameter, and null or Nothing to the id parameter.
The following example shows the default route definitions from the Global.asax file.
RouteTable.Routes.Add(New Route With { _
.Url = "{controller}/{action}/{id}", _
.Defaults = New With {.action = "Index", .id = CStr(Nothing)}, _
.RouteHandler = New MvcRouteHandler _
})
RouteTable.Routes.Add(New Route With { _
.Url = "default.aspx", _
.Defaults = New With {.controller = "Home", .action = "Index", .id = CStr(Nothing)}, _
.RouteHandler = New MvcRouteHandler _
})
RouteTable.Routes.Add(new Route
{
Url = "{controller}/{action}/{id}",
Defaults = new { action = "Index", id = (string)null },
RouteHandler = new MvcRouteHandler()
});
RouteTable.Routes.Add(new Route
{
Url = "Default.aspx",
Defaults = new { controller = "Home", action = "Index",
id = (string)null },
RouteHandler = new MvcRouteHandler()
});
For IIS 7.0, no file-name extension is needed. For IIS 6.0, you must include the .mvc file-name extension in the URL pattern, such as {controller}.mvc/{action}/{id}.
Mapping URLs to a Controller and Action
A URL request is routed through the UrlRoutingModule object and then through the MvcHandler HTTP handler. The MvcHandler HTTP handler determines which controller to invoke and which action method to call.
The MVC framework takes the value in the controller parameter and adds "Controller" to determine the name of the controller for the request. For example, a URL that includes the URL path /Products is mapped to a controller named ProductsController. The value in the action parameter is the name of the action method that is called. A URL that includes the URL path /Products/show would result in a call to the show method of the ProductsController class.
A controller class must implement the System.Web.MVC.IController interface, and the name of the controller must end with "Controller". The easiest way to implement a controller is to create a class that derives from the System.Web.MVC.Controller base class. You can then add methods to the controller class that are marked with the ControllerAction attribute and let the default URL route handling invoke the methods. If you follow this strategy, you can rely on the MVC framework to perform URL routing, and to expose the action methods that you have marked with the ControllerAction attribute.
This topic is ASP.NET 3.5 Extensions pre-release documentation and is unsupported by Microsoft. Blank topics are included as placeholders and existing content is subject to change in future releases. To provide feedback or ask questions about the release, please go to the ASP.NET 3.5 Extensions forums.