GPGVM
GPGVM

Reputation: 5619

Incorrect routing with optional parameter

I wanted to extend a controller action to accept an optional parameter. In other words I wanted existing code to continue to work but add some new functionality that would pass additional information. I modified my routeprovider, controller, and tested it. The optional parameter and routing all worked great. So I did a regression test to see that existing code would still work and you guessed it. It didn't.

So what am I doing wrong / not understanding? I thought I could use the existing controller action and it would work with or without the optional parameter?

I have not posted my controller code as I don't believe that is coming into play. I set my breakpoint on the first line and never get there so it seems to me my routeprovider has an error. If other code is needed I will gladly add it.

this works: domain.com/signup/12345

this doesn't but used to work just fine: domain.com/signup

routes.MapLocalizedRoute("SignUp", "signup/{CampaignId}",
new {controller="Customer", action="NewSignup", CampaignId=UrlParameter.Optional},
new { CampaignId = @"\d+" },
new[] { "Domain.PROJECT.Controllers" });

So I would like the two url samples above to work. I can of course add another route without the campaignid and point it to the same controller action but again...I THOUGHT...that was the idea behind making the campaignid optional???

Does the new { CampaignId = @"\d+" }, object constraint override the optional parameter and basically force the campaignid to exist?

Thank You

Upvotes: 1

Views: 357

Answers (1)

Andy T
Andy T

Reputation: 9881

Just define two routes.

One requiring the campaingId:

routes.MapLocalizedRoute("SignUpCampaign", "signup/{CampaignId}",
                         new { controller="Customer", action="NewSignup" },
                         new { CampaignId = @"\d+" },
                         new[] { "Domain.PROJECT.Controllers" });

And one not requiring it:

routes.MapLocalizedRoute("SignUp", "signup",
                         new { controller="Customer", action="NewSignup" },
                         null,
                         new[] { "Domain.PROJECT.Controllers" });

Upvotes: 1

Related Questions