【问题标题】:Multiple actions were found that match the request with multiple PUT webapi actions发现多个操作将请求与多个 PUT webapi 操作相匹配
【发布时间】:2013-08-30 21:36:02
【问题描述】:

我有这样的路由设置,以允许我的 webapi 控制器基于操作的路由:

config.Routes.MapHttpRoute("DefaultApiWithIdAndAction", "{controller}/{id}/{action}", null, new { id = @"\d+" });
config.Routes.MapHttpRoute("DefaultApiWithId", "{controller}/{id}", null, new {id = @"\d+"});
config.Routes.MapHttpRoute("DefaultApiWithAction", "{controller}/{action}");
config.Routes.MapHttpRoute("DefaultApiGet", "{controller}", new { action = "Get" }, 
    new { httpMethod = new HttpMethodConstraint(HttpMethod.Get) });
config.Routes.MapHttpRoute("DefaultApiPost", "{controller}", new {action = "Post"}, 
    new {httpMethod = new HttpMethodConstraint(HttpMethod.Post)});

以下是我想要支持的所有路线类型。除了没有操作的默认 PUT 之外,它们都可以工作。出于某种原因,基于操作的 PUT 请求可以正常工作。

GET  users
GET  users/1
POST users
PUT  users/1             <- thinks its a duplicate route
PUT  users/1/assignrole  <- of this route even though this one works
DEL  users/1

这是我定义控制器操作的方式:

public UserModel Put(int id, UserModel model)

[ActionName("assignrole")]
public UserModel PutAssignRole(int id, RoleModel model)

我原以为它们是不同的,因为动作名称不同,但 mvc 不是这样看的。我做错了什么?

【问题讨论】:

    标签: asp.net-mvc-4 asp.net-web-api duplicates asp.net-mvc-routing


    【解决方案1】:

    您需要区分这两种方法的签名。将您的第一条路线更改为:

    config.Routes.MapHttpRoute("DefaultApiWithIdAndAction",
        "{controller}/{id2}/{action}",
        null,
        new { id2 = @"\d+" });
    

    然后将您的第二个操作更改为:

    [ActionName("assignrole")]
    public UserModel PutAssignRole(int id2, RoleModel model)
    {
        ...
    }
    

    【讨论】:

      【解决方案2】:

      只是为了给大家一个更新,我已经放弃尝试使用传统的 webapi 路由来做到这一点。我已经使用 attributerouting.net 采用了基于属性的路由,因为它似乎是大多数人都在推动解决这个问题的解决方案。我之所以做出决定,主要是因为 attributerouting.net 功能正在被纳入 VS2013 版本的 WebAPI 2 中。语法略有不同,但功能几乎完全相同。这是一个巨大的进步。甚至 stackoverflow 也将它用于他们的路由,这更加坚定了我的决定。

      【讨论】:

        【解决方案3】:

        您可以尝试将第一个路由定义限制为仅接受这一操作,因为它似乎并没有用于任何其他路由:

        config.Routes.MapHttpRoute("DefaultApiWithIdAndAction", 
             "{controller}/{id}/{action}", 
             null, 
             new { id = @"\d+", action="assignrole" });
        

        【讨论】:

        • 这条路线仍然会给你“找到多个操作”错误。
        • 如果我只有一个遵循此模式的操作,那将起作用。我有很多,不得不把它们都列出来是一件痛苦的事。到目前为止,基于属性的路由一直是一个梦想。如此多的灵活性,而且设置起来也不像是负担。
        猜你喜欢
        • 1970-01-01
        • 2023-04-08
        • 2017-10-26
        • 2016-10-18
        • 2016-06-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-09-28
        相关资源
        最近更新 更多