views:

1036

answers:

1

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!)

+1  A: 

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.

Odd
+1 exactly what I was going to say
Robert Gould
Yes, but how about my search url that has about 15 optional parameters. And optional doesn't mean with a default, it means optional.I could off course agree on a certain term that my controller then discards when found.
borisCallens