views:

117

answers:

1

I want to define a route that have 2 optional parameters in the middle of the URL the start an end parameters are digits

routes.MapRoute(
                "",
                "Source/Changeset/{start}/{end}/{*path}",
                new {
                        controller = "Source",
                        action = "Changeset",
                        start = UrlParameter.Optional,
                        end = UrlParameter.Optional, 
                        path = "crl"
                    },
                new { start = @"\d+", end = @"\d+" }
                );

i tried diferent approaches and none of them worked, i could use some of your help.

Thanks in advance.

EDIT

I manage to solve the problem this way, but it's far from elegant.

routes.MapRoute(
                "",
                "Source/Changeset/{start}/{end}/{*path}",
                new {
                        controller = "Source",
                        action = "Changeset",
                        start = UrlParameter.Optional,
                        end = UrlParameter.Optional, 
                        path = "crl"
                    },
                new { start = @"\d+", end = @"\d+" }
                );  

            routes.MapRoute(
                "",
                "Source/Changeset/{start}/{*path}",
                new
                {
                    controller = "Source",
                    action = "Changeset",
                    start = UrlParameter.Optional,
                    path = "crl"
                },
                new { start = @"\d+" }
                );  

            routes.MapRoute(
                "",
                "Source/Changeset/{*path}",
                new
                {
                    controller = "Source",
                    action = "Changeset",
                    path = "crl"
                }
                );  
A: 

you are going to need to make routes for all possible combinations, which can get a little hairy:

//route if everything is included
routes.MapRoute(null, ""Source/Changeset/{start}/{end}/{*path}",
    new { controller = "Home", action = "Index" }
);

//route if nothing is included
routes.MapRoute(null, ""Source/Changeset/{*path}",
    new { controller = "Home", action = "Index", start=0, end=5 } //defaults
);

//and so on...

But, there is also a problem here: since start and end are both digits, there would be no way (since they are optional) to decide if the '2' in Source/Changeset/2/fodass is the start or end variable, so you may have to think up a new approach, or change yours to something like Source/Changeset/Start/2/fodass, Source/Changeset/End/5/fodass, Source/Changeset/Start/2/End/5/fodass, etc.

naspinski
if a value exists, it's the start, so no need to do that. thank you for your help.
fampinheiro