264Microsoft Visual Studio 2010: A Beginner’s Guide
Listing
Routing works by pattern matching, which you can see through the two statements in the RegisterRoutes method: IgnoreRoute and MapRoute. IgnoreRoute is useful for situations where you want to let IIS request the exact URL. In this case, it is any file with the *.axd extension, regardless of parameters.
The MapRoute method shows a common pattern for matching URLs to controllers, actions, and parameters. The first parameter is the name of the route. The second parameter describes the pattern, where each pattern match is defined between curly braces. Based on the URL, http://localhost:1042/Home/About, the pattern, {controller}/{action}/ {id}, matches Home to {controller} and About to {action}; there is no match for {id}. Therefore, ASP.NET MVC will append “Controller” to the URL segment that matches {controller}, meaning that the Controller name to instantiate is HomeController. About
is the method inside of HomeController to invoke. Since About doesn’t have parameters, supplying the {id} is unnecessary.
The third parameter for MapRoute specifies default values, where the key matches the pattern parameter and the value assigned to the key is what ASP.NET MVC uses when it doesn’t find a pattern match with the URL. Here are a couple of examples:
●http://localhost:1042 invokes the Index method of HomeController because no Controller or action matches and the defaults are Home for {controller} and Index for {action}.
●http://localhost:1042/Home invokes the Index method of HomeController because no action was specified and the default value for {action} is Index.
You can create your own custom route by using the MapRoute method and specifying other default values for the parameters.
Building a Customer Management Application
Now, we’ll pull together the ASP.NET MVC concepts you’ve learned and describe how to build a very simple application that displays, adds, modifies, and deletes customers. In so doing, you’ll see how to build up a Model that supports customers, how to create a custom Controller with actions for managing customers, and how to create multiple views to handle interaction with the users as they work with customers.