如果在其他控制器上没有定义更多特定路由(即“api / myaccount / 1 / feature”),我希望捕获匹配常规路由前缀(“api / myaccount / 1”)的所有路由“)但是当我这样做时,我得到以下异常:
找到了与URL匹配的多种控制器类型。这个可以 如果多个控制器上的属性路由匹配请求,则会发生 URL。
如上所述: Multiple controller types were found that match the URL. This can happen if attribute routes on multiple controllers match the requested URL似乎这可能是不可能的。
当找不到更好的路线时,想要执行默认路线听起来很常见,所以我错过了什么?我是否需要在管道中挂钩或什么东西......
仅供参考:我的捕获工作正常(“api / myaccount / 1 / {* uri}”)这只是能够覆盖它的问题。
答案 0 :(得分:4)
事实证明这很简单,我只需创建一个自定义Controller Selector并覆盖GetControllerName函数。需要特殊覆盖,因为您希望覆盖的方法:
"createdAt": {
"type": "date",
"defaultFn": "now"
}
不仅会返回描述符(如果找不到匹配项,则返回null),如您所料。该方法实际上为您处理请求并返回404:/然而,一旦您意识到这是一件容易的事情,我可以使用下面的代码获得我想要的行为:
HttpControllerDescriptor SelectController(HttpRequestMessage request)
并将其添加到您的配置中:
using System.Web.Http;
using System.Web.Http.Dispatcher;
public class CustomControllerSelector : DefaultHttpControllerSelector
{
public override string GetControllerName(HttpRequestMessage request)
{
var name = base.GetControllerName(request);
if(string.IsNullOrEmpty(name))
{
return "MyFeature"; //important not to include "Controller" suffix
}
return name;
}
}