【发布时间】:2016-07-24 01:05:35
【问题描述】:
我一直在编写单元测试来验证 MVC 路由的有效性。例如,
[Route("foo/bar/{id:int}")]
public IHttpActionResult SomeAction(int id)
{
// ...
}
应该是有效的,但是
[Route("foo/bar/{id:List<string>}")]
public IHttpActionResult SomeAction(int id)
{
// ...
}
或
[Route("foo/bar/{buzz:int}")]
public IHttpActionResult SomeAction(int id)
{
// ...
}
不会。
但问题是我得到了误报,因为在上面的第一个示例中,当我检查路由中的 {int:id} 是否与操作方法中的参数匹配时,参数中的类型名称是 @ 987654327@ 通过反射,我不知道如何让我的测试知道Int32 与int 相同。有办法吗?
【问题讨论】:
-
这 100% 肯定不是手头的问题。你需要深入挖掘。
-
您对路由配置中
int的含义做出了错误的假设。int是 route constraint 的名称,它与int数据类型(别名为System.Int32)的 C# 含义无关。此外,你应该是testing your routing config,而不是测试MVC框架是否工作(你应该假设它已经过测试)。
标签: c# .net asp.net-mvc reflection asp.net-mvc-routing