如何向路由参数添加约束?

时间:2010-11-12 22:31:09

标签: asp.net-mvc regex routing constraints

我想为我拥有的路由参数添加约束。路线模式是:

{region}/{controller}/{action}

其中{region}应该只有UKUS

我该怎么做?我可以在这里应用正则表达式,还是其他一些定义它的方法?

1 个答案:

答案 0 :(得分:2)

之前已经问过这个问题。参见:

Constraint Question

实质上,使用正则表达式:

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