Can I constrain a route parameter to a certain typ

2019-08-26 16:46发布

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条回答
该账号已被封号
2楼-- · 2019-08-26 17:34

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.

查看更多
孤傲高冷的网名
3楼-- · 2019-08-26 17:40

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
);
查看更多
登录 后发表回答