MVC 4路由:由于api \ controller \ action \ id和api \ controller \ id引起的多个匹配错误

时间:2012-09-27 20:43:16

标签: asp.net-mvc-4 asp.net-mvc-routing

我正在尝试设置路由配置,并且遇到异常“找到了与请求匹配的多个操作:”

当我转到/ api / Devices / {id}

时出现错误

我知道罪魁祸首方法 - GetParts。为什么它仍然作为控制器/ id的可能匹配触发?我如何让它停止,或者只使用get方法?

    public Devices Get(int id)
    {
        return Uow.Distribution.Devices.GetById(id);
    }

    [ActionName("GetParts")]
    public IEnumerable<DeviceParts> GetParts(int id)
    {
        return Uow.Distribution.Devices.GetParts(id);
    }

我的路线

        routes.MapHttpRoute(
            name: ControllerOnly,
            routeTemplate: "api/{controller}"
            );

        routes.MapHttpRoute(
            name: ControllerAndId,
            routeTemplate: "api/{controller}/{id}",
            defaults: null,
            constraints: new { id = @"^\d+$" }
        );

        routes.MapHttpRoute(
            name: ControllerAction,
            routeTemplate: "api/{controller}/{action}"
        );

        routes.MapHttpRoute(
            name: ControllerActionAndId,
            routeTemplate: "api/{controller}/{action}/{id}",
            defaults: new { id = RouteParameter.Optional },
            constraints: new { id = @"^\d+$" }
        );

由于

0 个答案:

没有答案