views:

16

answers:

1

I have a route with several optional parameters. These are possible search terms in different fields. So, for example, if I have fields key, itemtype and text then I have in global.asax:

routes.MapRoute( _
        "Search", _
        "Admin.aspx/Search/{Key}/{ItemType}/{Text}", _
        New With {.controller = "Admin", .action = "Search" .Key = Nothing, .ItemType = Nothing, .Text = Nothing} _
    )

My action takes optional parameters:

Function Search(Optional ByVal Key As String = Nothing, _
                Optional ByVal ItemType As Integer = 0, _
                Optional ByVal Text As String = Nothing, _
                Optional ByVal OtherText As String = Nothing)

It then checks if the Key and Text strings have a non-null (and non-empty) value and adds search terms to the db request as needed.

However, is it possible to send in a null value for, for example, Key but still send in a value for Text? If so, what does the URL look like? (Admin.aspx/Search//0/Foo doesn't work :) )

I know I can handle this using a parameter array instead, but wondered if this was possible using the sort of route described? I could of course define some other value as equivalent to null (for example, a space/%20) but is there any way to send a null value in the URL? I'm suspecting not, but thought I'd see if anyone knew of one.

I'm using ASP MVC 2 for this project.

A: 

In your route, use UrlParameter.Optional as your default value:

routes.MapRoute( _
    "Search", _
    "Admin.aspx/Search/{Key}/{ItemType}/{Text}", _
    New With { .controller = "Admin", .action = "Search", _
               .Key = UrlParameter.Optional, .ItemType =  UrlParameter.Optional, _ 
               .Text = UrlParameter.Optional } _
)
Tomas Lycken