0% found this document useful (0 votes)
7 views9 pages

Using The Default Route Table: Videos Samples Forum Books Open Source

How to Rooting ASP Page

Uploaded by

Kevin R Agustian
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views9 pages

Using The Default Route Table: Videos Samples Forum Books Open Source

How to Rooting ASP Page

Uploaded by

Kevin R Agustian
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Blog

[Link]
MVC: Guidance Videos Samples Forum Books Open Source

Older Versions - MVC 1 and 2 +


[Link] MVC Routing Overview (C#)
By Stephen Walther | August 19, 2008
135 of 165 people found this helpful
Like 18 Print

In this tutorial, Stephen Walther shows how the [Link] MVC framework maps browser requests to controller actions.

In this tutorial, you are introduced to an important feature of every [Link] MVC application called [Link] Routing. The [Link]
Routing module is responsible for mapping incoming browser requests to particular MVC controller actions. By the end of this tutorial, you
will understand how the standard route table maps requests to controller actions.

Using the Default Route Table


When you create a new [Link] MVC application, the application is already configured to use [Link] Routing. [Link] Routing is
setup in two places.

open in browser PRO version Are you a developer? Try out the HTML to PDF API [Link]
First, [Link] Routing is enabled in your application's Web configuration file ([Link] file). There are four sections in the
configuration file that are relevant to routing: the [Link] section, the [Link] section, the
[Link] section, and the [Link] section. Be careful not to delete these sections because without
these sections routing will no longer work.

Second, and more importantly, a route table is created in the application's [Link] file. The [Link] file is a special file that
contains event handlers for [Link] application lifecycle events. The route table is created during the Application Start event.

The file in Listing 1 contains the default [Link] file for an [Link] MVC application.

Listing 1 - [Link]

using System;
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];

namespace MvcApplication1
{
// Note: For instructions on enabling IIS6 or IIS7 classic mode,
// visit [Link]

public class MvcApplication : [Link]


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

[Link](
open in browser PRO version Are you a developer? Try out the HTML to PDF API [Link]
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = "" } // Parameter defaults
);

protected void Application_Start()


{
RegisterRoutes([Link]);
}
}
}

When an MVC application first starts, the Application_Start() method is called. This method, in turn, calls the RegisterRoutes() method.
The RegisterRoutes() method creates the route table.

The default route table contains a single route (named Default). The Default route maps the first segment of a URL to a controller name,
the second segment of a URL to a controller action, and the third segment to a parameter named id.

Imagine that you enter the following URL into your web browser's address bar:

/Home/Index/3

The Default route maps this URL to the following parameters:

controller = Home

action = Index

open in browser PRO version Are you a developer? Try out the HTML to PDF API [Link]
id = 3

When you request the URL /Home/Index/3, the following code is executed:

[Link](3)

The Default route includes defaults for all three parameters. If you don't supply a controller, then the controller parameter defaults to the
value Home. If you don't supply an action, the action parameter defaults to the value Index. Finally, if you don't supply an id, the id
parameter defaults to an empty string.

Let's look at a few examples of how the Default route maps URLs to controller actions. Imagine that you enter the following URL into your
browser address bar:

/Home

Because of the Default route parameter defaults, entering this URL will cause the Index() method of the HomeController class in Listing 2
to be called.

Listing 2 - [Link]

using [Link];

namespace [Link]
{
[HandleError]
public class HomeController : Controller
{
public ActionResult Index(string id)
{
return View();
}
open in browser PRO version Are you a developer? Try out the HTML to PDF API [Link]
}
}

In Listing 2, the HomeController class includes a method named Index() that accepts a single parameter named Id. The URL /Home
causes the Index() method to be called with an empty string as the value of the Id parameter.

Because of the way that the MVC framework invokes controller actions, the URL /Home also matches the Index() method of the
HomeController class in Listing 3.

Listing 3 - [Link] (Index action with no parameter)

using [Link];

namespace [Link]
{
[HandleError]
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
}
}

The Index() method in Listing 3 does not accept any parameters. The URL /Home will cause this Index() method to be called. The URL
/Home/Index/3 also invokes this method (the Id is ignored).

The URL /Home also matches the Index() method of the HomeController class in Listing 4.
open in browser PRO version Are you a developer? Try out the HTML to PDF API [Link]
Listing 4 - [Link] (Index action with nullable parameter)

using [Link];

namespace [Link]
{
[HandleError]
public class HomeController : Controller
{
public ActionResult Index(int? id)
{
return View();
}
}
}

In Listing 4, the Index() method has one Integer parameter. Because the parameter is a nullable parameter (can have the value Null), the
Index() can be called without raising an error.

Finally, invoking the Index() method in Listing 5 with the URL /Home causes an exception since the Id parameter is not a nullable
parameter. If you attempt to invoke the Index() method then you get the error displayed in Figure 1.

Listing 5 - [Link] (Index action with Id parameter)

using [Link];

namespace [Link]
{
[HandleError]
open in browser PRO version Are you a developer? Try out the HTML to PDF API [Link]
public class HomeController : Controller
{
public ActionResult Index(int id)
{
return View();
}
}
}

Figure 01: Invoking a controller action that expects a parameter value (Click to view full-size image)

The URL /Home/Index/3, on the other hand, works just fine with the Index controller action in Listing 5. The request /Home/Index/3
causes the Index() method to be called with an Id parameter that has the value 3.

Summary
The goal of this tutorial was to provide you with a brief introduction to [Link] Routing. We examined the default route table that you get
with a new [Link] MVC application. You learned how the default route maps URLs to controller actions.

This article was originally created on August 19, 2008

Like 18

open in browser PRO version Are you a developer? Try out the HTML to PDF API [Link]
Author Information

Stephen Walther – Stephen Walther has been involved with [Link] from the beginning. His training company,
[Link] and [Link], conducted the first training class on [Link]. He also lectures
regularly on [Link] and he is a Microsoft [Link] MVP.

You're Viewing → Next


[Link] MVC Routing Overview (C#) Understanding Action Filters (C#)

Comments (0)
You must be logged in to leave a comment.

This site is managed for Microsoft by Neudesic, LLC. | © 2015 Microsoft. All rights reserved.

Privacy Statement | Terms of Use | Contact Us | Advertise With Us | CMS by Umbraco | Hosted on Microsoft Azure

open in browser PRO version Are you a developer? Try out the HTML to PDF API [Link]
Feedback on [Link] | File Bugs | Support Lifecycle

open in browser PRO version Are you a developer? Try out the HTML to PDF API [Link]

You might also like