ASP.NET MVC Framework (Part 2): URL Routing
page 2 of 8
by Scott Guthrie
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 43752/ 75

Default ASP.NET MVC URL Routing Rules

By default when you use Visual Studio to create a new project using the "ASP.NET MVC Web Application" template it adds an ASP.NET Application class to the project.  This is implemented within the Global.asax code-behind:

Figure 2

The ASP.NET Application class enables developers to handle application startup/shutdown and global error handling logic. 

The default ASP.NET MVC Project Template automatically adds an Application_Start method to the class and registers two URL Routing rules with it:

Figure 3

The first routing rule above indicates that the ASP.NET MVC framework should by default map URLs to Controllers using a "[controller]/[action]/[id]" format when determining which Controller class to instantiate, and which Action method to invoke (along with which parameters should be passed in). 

This default routing rule is why a URL request for /Products/Detail/3 in our e-commerce browsing sample from Part 1 automatically invokes the Detail method on our ProductsController class and passes in 3 as the id method argument value:

Figure 4

The second routing rule above is added to special-case the root "Default.aspx" URL in our application (which is sometimes passed by web servers in place of "/" when handling requests for the root URL of an application).  This rule ensures that requests for either the root "/Default.aspx" or "/" to our application are handled by the "Index()" action on the "HomeController" class (which is a controller automatically added by Visual Studio when we created a new application using the "ASP.NET MVC Web Application" project template).


View Entire Article

User Comments

Title: The second routing rule above is added to special-case the root "Default.aspx" URL in our application (which is sometimes passed by web servers in pla   
Name: The second routing rule above is added to special-case the root "Default.aspx" URL in our application (which is sometime
Date: 2009-02-12 4:24:01 AM
Comment:
The second routing rule above is added to special-case the root "Default.aspx" URL in our application (which is sometimes passed by web servers in place of "/" when handling requests for the root URL of an application). This rule ensures that requests for either the root "/Default.aspx" or "/" to our application are handled by the "Index()" action on the "HomeController" class (which is a controller automatically added by Visual Studio when we created a new application using the "ASP.NET MVC Web Application" project templateThe second routing rule above is added to special-case the root "Default.aspx" URL in our application (which is sometimes passed by web servers in place of "/" when handling requests for the root URL of an application). This rule ensures that requests for either the root "/Default.aspx" or "/" to our application are handled by the "Index()" action on the "HomeController" class (which is a controller automatically added by Visual Studio when we created a new application using the "ASP.NET MVC Web Application" project templateThe second routing rule above is added to special-case the root "Default.aspx" URL in our application (which is sometimes passed by web servers in place of "/" when handling requests for the root URL of an application). This rule ensures that requests for either the root "/Default.aspx" or "/" to our application are handled by the "Index()" action on the "HomeController" class (which is a controller automatically added by Visual Studio when we created a new application using the "ASP.NET MVC Web Application" project templateThe second routing rule above is added to special-case the root "Default.aspx" URL in our application (which is sometimes passed by web servers in place of "/" when handling requests for the root URL of an application). This rule ensures that requests for either the root "/Default.aspx" or "/" to our application are handled by the "Index()" action on the "HomeController" class (which is a controller aut
Title: MVC website   
Name: Harendra chauhan
Date: 2009-01-27 2:15:12 AM
Comment:
I have ASP .net MVC website created on my machine ,it is runnig fine on my machine ,all links are working but when I hosted is on IIS 5.1 that time home page is assible from other pc on network but all its Links(url routing) is not working,Images are not getting loaded , css is not working.. Plz provide the help if a any... How can make my web app workable ?
Title: Problem Routing Solution   
Name: AMS
Date: 2008-10-30 2:27:16 PM
Comment:
lobo,

URL encode the product id when it goes in the url then decode it on the way back out. Should clean it up for you
Title: Problem routing   
Name: Lobo
Date: 2008-09-29 2:30:48 PM
Comment:
If you have products id like "7*"
The url used to view this product: /Products/Detail/7*
But this url generate an error... "Illegal characters in path"

How can i resolve this?

Product Spotlight
Product Spotlight 





Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-04-25 1:14:31 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search