Skip to Main Content
ASP.NET 4 24-Hour Trainer
book

ASP.NET 4 24-Hour Trainer

by Toi B. Wright
July 2010
Beginner content levelBeginner
552 pages
10h 14m
English
Wrox
Content preview from ASP.NET 4 24-Hour Trainer

Chapter 15B. URL Routing in MVC

As you learned in Lesson 8B, URL routing is a critical component of any ASP.NET MVC application. Routes are used to map incoming browser requests to their relevant controller action methods. In this lesson I show you how routing is used in an ASP.NET MVC web application, and I show you how to add a custom route.

When you create a new application using the ASP.NET MVC 2 Web Application template, a default RegisterRoutes method is created in the Global.asax page. This is the code in the default RegisterRoutes method:

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    routes.MapRoute(
        "Default",                         // Route name
        "{controller}/{action}/{id}",      // URL
        new { controller = "Home",
              action = "Index",
              id = UrlParameter.Optional}  // Defaults
    );
}

The first line of the RegisterRoutes method includes code to ignore any routes that include a file with the .axd file extension. You can use the routes.IgnoreRoute method to have routing ignore any files in your application.

An ASP.NET MVC web application uses the MapRoute method of the RouteCollection class to define new routes. The MapRoute method can include the following parameters:

  • name — This is a string representing the name of the route. This is required.

  • url — This is a string that contains the URL pattern for the route. This is required.

  • defaults — This is an object containing the default values for the route.

  • constraints — This is an object containing the constraints ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

ASP.NET 4 24-Hour Trainer

ASP.NET 4 24-Hour Trainer

Toi B. Wright
ASP.NET 4 Unleashed

ASP.NET 4 Unleashed

Stephen Walther, Kevin Hoffman, Nate Dudek

Publisher Resources

ISBN: 9780470596913Purchase bookExamplesErrata