Web API路由机制是否足够智能以接受多个Controller名称?

时间:2014-07-09 23:58:29

标签: asp.net-web-api controller asp.net-web-api-routing

我在DeliveryController.cs中有这样的方法:

[Route("api/Deliveries/Count")]
public int GetCountOfDeliveryRecords()
{
    return _deliveryRepository.GetCount();
}

还有其他方法使用“交付”而不是发现的“交付”。但为什么还找到了控制器名称的复数? Web API路由引擎是否真的首先准确地查找API调用,然后,如果找不到,请查找其中的单数?

IOW,当传递“... api / Deliveries / Count”时,它首先查找DeliveriesController,当找不到时,然后搜索DeliveryController?

1 个答案:

答案 0 :(得分:1)

当您将Route属性直接应用于方法时,路由引擎确切地知道通过反射映射到该路由的方法的名称是什么,并且它不会尝试找到它基于它的名字。

使用属性路由时,即使您不遵守约定,也可以使用您想要的任何命名。

此路线完全有效:

[Route("api/whatever")]
public int UnrelatedName()
{
    return _deliveryRepository.GetCount();
}