A brief summary of the first part
In the first part of the series, we created an ecommerce web site that showed three types of URLs:
URL format |
Behavior |
URL Example |
/products/categories |
Browse All Product Categories |
/products/categories |
/products/list/category |
List the products in a category |
/products/list/beverages |
/products/detail/productid |
Show details of a particular product |
/products/detail/34 |
We process these URLs by creating a ProductsController class like the following:
After adding the above class to our application, the ASP.net MVC framework automatically directs incoming URLs to the appropriate action method on our controller to handle the request.
In today's post, we'll delve into how this URL mapping happens, and explore the more advanced path selection (routing) scenarios that we can leverage in the ASP.net MVC framework. I will also demonstrate how you can easily select a scene for the unit test URL path.
What does the ASP.net MVC URL path selection system do?
The ASP.net MVC framework includes a flexible URL path selection system that allows you to define URL mapping rules in your application. The path selection system has 2 main purposes:
Mapping incoming URLs to applications and directing them so that the correct controller and action methods are executed to handle these requests
Build URLs that can be used to callback controllers/actions to the client (for example, form submission, <a href= "" > Links, and AJAX calls, etc.)
The ability to use URL mapping rules to handle both incoming and outgoing URL scenarios adds a lot of flexibility to the application code. This means that if we want to change the URL structure of the application in the future (for example, by renaming/products to/catalog), we can modify a set of mapping rules for the application hierarchy without altering any code in the controller or view template.
Default asp.net MVC URL path selection rule
By default, when you use Visual Studio to create a new project with the ASP.net MVC Web application template, it adds a ASP.net application class to the project. This is implemented in the Global.asax background code: