ASP.NET MVC Routing – Default Values

Default values in ASP.NET MVC Routing allow for much more flexible URL pattern matching. In our previous   MVC Routing Tutorial we introduced Routing and how it matches a URL to a predefined Route and then extracts the parameters for processing. In our original simple examples, a URL segment had to be provided for each parameter in a URL Route.

So, to recap, if we defined a URL Route for handling incoming URLs for a content  management system  which needs to display the articles for a given category then the route may look as below:

routes.MapRoute(“simple”, “{controller}/{action}/{id}“);

and the URL of /articles/category/15 would result in the below controller (with 15 being passed in as the id parameter).

public class ArticlesController : Controller
{
public ActionResult Category(int id)
{
//Perform Operations....
}
}

This url would therefore  output the articles for Category ID 15. But what if we wanted to use the URL /articles/category/ to display all the articles irrespective of category. For this we would need to use default values in Routing.

The provision of a default value in the Route definition allows ASP.NET MVC Routing to provide a preset value when none is provided in the URL (in a  similar fashion to default values for parameters in function definitions).

The default value needs to be in the Route definition:

routes.MapRoute(“simple”, “{controller}/{action}/{id}“, new {id = 0});

Now, the URL /articles/category/ will be interpretted as /articles/category/0 and so in this example the 0 value for id can be used to display all articles regardless of category.

Multiple Default Values

Multiple default values can be used for different parameters as shown below:

routes.MapRoute(“simple” , “{controller}/{action}/{id}“,
new {id = 0, action=”home”});

In this example the URL /articles/category/ would behave as shown previously but the URL /articles/ would behave as /articles/home/ ( and so in the example of a content management system the URL /articles/category/ would be the articles home page).

One rule to bear in mind when using default values is the if a default is defined for a parameter then all subsequent parameters must have a default value as well. For example :

routes.MapRoute(“simple” , “{controller}/{action}/{id}“,
new { action=”home”});

is not valid and a default for the id parameter needs to be assigned.

Another caveat in using default values is that defaults cannot be used when literal values are used in parameters. For example the Route,

routes.MapRoute(“simple” , “{controller}/{action}-{id}”);

uses the literal string “  - ” to separate {controller} and {action} which would match URLs such as /articles/category-12/ . In this case a default cannot be used for action or id .

2 thoughts on “ASP.NET MVC Routing – Default Values

  1. Pingback: ASP.NET MVC Routing Tutorial – Part I | ASP.NET 101

  2. Pingback: ASP.NET 4.0 Routing Tutorial | ASP.NET 101

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>