DaveDev
DaveDev

Reputation: 42185

How to add a constraint to a route parameter?

I'd like to add a constraint to a route parameter that I have. The route pattern is:

{region}/{controller}/{action}

where {region} should only be UK or US.

How can I do this? Is there a regex I can apply here, or some other means of defining this?

Upvotes: 0

Views: 1788

Answers (1)

awrigley
awrigley

Reputation: 13581

This question has been asked before. See:

Constraint Question

In essence, use Regular Expressions:

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
);

Upvotes: 2

Related Questions