views:

39

answers:

2

I have the following route:

routes.MapRoute(
    "Search",                                               // Route name
    "Search/{affiliateId}",                                 // URL with parameters
    new { controller = "Syndication", action = "Search" }   // Parameter defaults
);

Is there a way I can ensure "affiliateId" is a valid Guid? I'm using MVCContrib elsewhere in my site and I'm fairly it provides a way to implement this kind of constraint.... I just don't know what it is!

+2  A: 

You could write regex constraints:

routes.MapRoute(
    "Search",                                               // Route name
    "Search/{affiliateId}",                                 // URL with parameters
    new { controller = "Syndication", action = "Search" },   // Parameter defaults
    new { affiliateId = "SOME REGEX TO TEST GUID FORMAT" } // constraints
);
Darin Dimitrov
Hmm, thanks. I was hoping for something a little less brittle though.
Paul Suart
@Paul Suart: If `affiliateId` is just a numeric parameter, than your regex is simply `"\d"` and you're good to go. Why do you think it's brittle?
Robert Koritnik
It's a guid, so the regex is more complex than "\d". Thanks anyway.
Paul Suart
Darin Dimitrov
A: 

I've never heard of this. I fear it would cause some confusion if you mistakenly used the wrong type for the affiliateId parameter in one of your action methods.

Ryan
I don't think it's uncommon to constrain a route to a certain structure. As another poster states, I could use a regex which is a known and documented way of ensuring the route is only matched when the url is of a certain format. I'm a nice guy so no -1 :)
Paul Suart
Ok after seeing your clarification above I believe I misunderstood you. Are you looking to make two routes, like `Search\{affiliateId}` and `Search\{query}`? If so I think the regex route is best. Alternatively your Search action could accept a string parameter and then call the appropriate code for searching vs affiliates.
Ryan