Boris Callens
Boris Callens

Reputation: 93287

Is there a way to send optional parameters to an action?

Can I send optional parameters (empty strings, null int?'s etc) to an action through a GET request in asp.net mvc? (one sentence question!)

Upvotes: 6

Views: 1928

Answers (1)

Odd
Odd

Reputation: 4757

You can do optional parameters with the routing table fairly easily, just specify the defaults in the route of the global.cs file.

So for a search page with an optional query and page you would have something like

RouteTable.Routes.Add(new Route
{
    Url = "Search/[query]/[page]",
    Defaults = new { controller="Search", action="Results", page=1 },
    RouteHandler = typeof(MvcRouteHandler)
});

Default page for your search is then 1.

This example is found here on Scott Gu's blog.

Upvotes: 2

Related Questions