我可以约束路由参数,以某型在ASP.net MVC? [英] Can I constrain a route parameter to a certain type in ASP.net MVC?

查看:146
本文介绍了我可以约束路由参数,以某型在ASP.net MVC?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下途径:

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

有没有一种方法,我可以保证AFFILIATEID是一个有效的GUID?我使用MVCContrib其他地方我的网站,我相当它提供了一种方式来实现这种约束....我只是不知道它是什么!

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!

推荐答案

您可以写正则表​​达式约束:

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

这篇关于我可以约束路由参数,以某型在ASP.net MVC?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆