0
使用MVC的WebAPI,我有一個url,我想用於GET和POST。Web API如何擁有一個具有多個操作的路由
例子:
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
}
這甚至可能嗎?謝謝。
奏效!同樣,我必須確保控制器只有1個GET操作,否則,MVC Web API不知道要使用哪一個。很高興知道。 –