Asp.net web api Web API如何使用一个路由和多个操作
使用MVC的WebAPI,我有一个url,我想用于GET和POST 例如:Asp.net web api Web API如何使用一个路由和多个操作,asp.net-web-api,Asp.net Web Api,使用MVC的WebAPI,我有一个url,我想用于GET和POST 例如: 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",
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
时,我发现错误在控制器“设备”上找不到与名称“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
}
这可能吗?谢谢。不要设置操作:
config.Routes.MapHttpRoute(
name: "Person-GetAndPost",
routeTemplate: "person",
defaults: new { controller = "Person" }
);
成功了!尽管如此,我必须确保控制器中只有1个GET操作,否则MVCWebAPI不知道使用哪一个。很高兴知道。