ASP.NET Routing in MVC Applications
Introduction
The ASP.NET MVC framework uses ASP.NET routing to map URLs to controller classes and actions. ASP.NET 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 routing with MVC. For general information about how to define routes and construct URLs dynamically, see ASP.NET Routing.
Adding Routes in MVC
The Add method of the RouteCollection class is used to add routes. In addition, the MVC framework provides the MapRoute method and the IgnoreRoute method for adding routes to an MVC application. These methods are extension methods of the RouteCollection class; neither method is available outside MVC applications.
The MapRoute method simplifies the syntax for adding a route because you do not have to create a new instance of a Route object before you add it to the RouteCollection object. Instead, you specify the properties for the Route object that you want to add, and the MapRoute method creates the Route instance for you.
The IgnoreRoute method enables you to exclude certain requests from being processed by routing. Any route that is added through the IgnoreRoute method will be handled by the StopRouteHandler class. The StopRouteHandler class blocks additional processing of the request by routing, and lets the request fall through for ASP.NET endpoint processing.
Default Routes
Visual Studio project templates for ASP.NET MVC include preconfigured URL routes. These are defined in the ASP.NET Application class in the Global.asax file. The preconfigured routes let you start on an ASP.NET MVC application without having to explicitly configure routes for the most common scenarios.
The following table shows the default routes that are included in an MVC application.
Shared Sub RegisterRoutes(ByVal routes As RouteCollection)
' Note: Change the URL to "{controller}.mvc/{action}/{id}" to enable
' automatic support in IIS 6.0 and in IIS 7.0 classic mode.
' Handler to stop URL routing for Web resources.
routes.IgnoreRoute("{resource}.axd/{*pathInfo}")
' MapRoute takes the following parameters, in order:
' (1) Route name
' (2) URL with parameters
' (3) Parameter defaults
' (4) Parameter constraints
routes.MapRoute( _
"Default", _
"{controller}/{action}/{id}", _
New With {.controller = "Home", .action = "Index", .id = ""} _
)
End Sub
public static void RegisterRoutes(RouteCollection routes)
{
// Note: Change the URL to "{controller}.mvc/{action}/{id}" to enable
// automatic support in IIS 6.0 and in IIS 7.0 classic mode.
// Handler to stop URL routing for Web resources.
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = "" } // Parameter defaults
);
}
The route with the pattern {resource}.axd/{*pathInfo} is included to prevent requests for the Web resource files such as WebResource.axd or ScriptResource.axd from being passed to a controller.
The following table shows examples of the type of URL requests that are handled by the default routes.
|
Default URL pattern
|
Examples of matching URL
|
|
{controller}/{action}/{id}
|
http://server/application/Products/show/beverages
http://server/application/
|
|
{resource}.axd/{*pathInfo}
|
http://server/application/WebResource.axd?d=...
|
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, as in the following example:
{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 adds the suffix "Controller" to the controller value in the URL to determine the type name of the controller that will handle 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.
The easiest way to implement a controller is to create a class that derives from the System.Web.Mvc.Controller base class and give it a name that ends with "Controller". You can then add public methods to the controller class 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.
This topic is ASP.NET Extensions documentation and is unsupported by Microsoft. Blank topics are included as placeholders and existing content is subject to change in future releases.