The responsibility of the ASP. NET Routing module is to map incoming browser requests to the unique MVC controller actions.
When we do not use a route, request http: // server/application/Produt. aspx? Id = 4. The server requests the Produt. aspx page and carries a parameter named id.
When a route is used and the same request is sent, the Produt. aspx page may not exist. For example, for http: // server/application/Products/show/beverages requests, the route parser processes Products show beverages. If the route is based on the path rule server/applicaton/{area}/{action}/{category}, it will find that Productes is area, it is a controller, and action corresponds to show, there is no doubt that beverages is categrory, so that the page can be correctly displayed.
2. Configure Route
When a new ASP. net mvc application is created, the application is configured with Routing in two places.
2.1 web. config
The configuration file contains four code snippets related to routing: system. web. httpModules code segment, system. web. httpHandlers code segment, system. webserver. modules code segment and system. webserver. handlers code segment. Do not delete these code segments. If they are not, routing will no longer run.
2.2 Global. asax
It contains event handlers of ASP. NET application lifecycle events. This route table will be created in the starting event of the application.
The default route will not waste your time here, so easy. Next let's take a look at how to customize Routes
3. Routing Overview
In a route, place a placeholder in braces to define ({and }). When the URL is parsed, the symbols "/" and "." are parsed as a delimiter, and the values between the delimiters match the placeholder. The information in the route definition that is not in braces is used as a constant value.
Route definition |
Example of matching URL |
{Controller}/{action}/{id} |
/Products/show/beverages |
{Table}/Details. aspx |
/Products/Details. aspx |
Blog/{action}/{entry} |
/Blog/show/123 |
{Reporttype}/{year}/{month}/{day} |
/Sales/2008/1/5 |
{Locale}/{action} |
/US/show |
{Language}-{country}/{action} |
/En-US/show |
4. Custom Routes
In the Global. asax file, use custom route to modify the default route table.
4.1 location: the custom Routes should be placed before the default route.
4.2 Example
Routes. IgnoreRoute (, {controller =, action =, id = UrlParameter. Optional}, {controller =, action =, id = UrlParameter. Optional} Database. DefaultConnectionFactory = SqlConnectionFactory (RegisterRoutes
When the program is started and Application_Start (), register the route table information RegisterRoutes
,},{,,},{,RegisterRoutes
References: http://msdn.microsoft.com/en-us/library/cc668201.aspx