0

使用 MVC 的 WebAPI,我有 1 个我想用于 GET 和 POST 的 url。

例子:

GET /person?personId=5
POST /person - the post would contain info about the new person to post, like { firstName: "Bob" }

我的路线:

config.Routes.MapHttpRoute(
  name: "Person-GetAndPost",
  routeTemplate: "person",
  defaults: new { controller = "Person", action = "Get|Post" }
);

在我的浏览器中访问person?personId=5时,我收到错误消息No action was found on the controller 'Device' that matches the name 'Get|Post'.

以下是我的控制器中的操作:

// with the action name called "Get", MVC Web API should match the verb GET with this action
public MyModels.Person Get(int personId)
{
    return; // return the person
}

// with the action name called "Post", MVC Web API should match the verb POST with this action
public MyModels.Person Post(MyModels.Person person)
{
    return; // return the updated person
}

这甚至可能吗?谢谢。

4

1 回答 1

0

不要设置动作:

config.Routes.MapHttpRoute(
  name: "Person-GetAndPost",
  routeTemplate: "person",
  defaults: new { controller = "Person" }
);
于 2013-09-26T17:42:36.947 回答