asp mvc routing with two optional parameters

Hi How do I map the url ../Companies/Results/value/id when both the parameters are optional?

Companies is the controller, Results is the action, value and id are optional parameters. On my form is a textbox for value and a selectlist for an id. The user could select both or one of each to search by. Tried something like this but cant handle when one of the optional parameters, say value, is missing such as ../Companies/Results/ /id

        routes.MapRoute(
            "Company+Profession", // Route name
            "{action}/{value}/{profId}", // URL with parameters
            new { controller = "Companies", action = "Index", value = UrlParameter.Optional, profId = UrlParameter.Optional } // Parameter defaults
        );

        routes.MapRoute(
            "Default", // Route name
            "{controller}/{action}/{id}", // URL with parameters
            new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
        );

You can't have a route that has two optional parameters, only the last parameter can be optional precisely because of the problem you describe. I suggest that you have a default parameter for value, like byid and use this when the person selects a profession.

I assume that you're constructing the URL via javascript, since using a GET form action would result in the parameter names being added to the URL. In this case, when the textbox is empty simply insert the default byid.

Update your route to include the default so any URLs that you generate will work. See Phil Haack's blog post on this for an alternative way to handle generating URLs that have two "optional" parameters.

// used when both parameters are specified
routes.MapRoute(
        "Company+Profession", // Route name
        "{action}/{value}/{profId}", // URL with parameters
        new { controller = "Companies", action = "Index", value ="byid", profId = UrlParameter.Optional } // Parameter defaults
);

Thanks guys, just discovered route constraints for integer. And so fiddling around with some combinations of routes it seems to work the way I want :

        routes.MapRoute(
           "Detail", // Route name
           "{action}/{value}", // URL with parameters
           new { controller = "Companies", action = "Detail" }, // Parameter defaults
           new { value = @"\d+" } //integer only
        );

        routes.MapRoute(
            "Company + Profession", // Route name
            "{action}/{value}/{profId}", // URL with parameters
            new { controller = "Companies", action = "Results" }, // Parameter defaults
            new { profId = @"\d+" } //integer only
        );

        routes.MapRoute(
            "Profession", // Route name
            "{action}/{profId}", // URL with parameters
            new { controller = "Companies", action = "Results"}, // Parameter defaults
            new {profId = @"\d+" } //integer only
        );

        routes.MapRoute(
           "Company", // Route name
           "{action}/{value}", // URL with parameters
           new { controller = "Companies", action = "Results" } // Parameter defaults
       );

        routes.MapRoute(
            "RootFolder", // Route name
            "{action}/{value}", // URL with parameters
            new { controller = "Companies", action = "Index", value = UrlParameter.Optional } // Parameter defaults
        );