C# mvcwebapi中带查询参数的投递路由
在Web Apì项目中,我想使用以下内容: 职位/我的控制员/1 但也包括POST/mycontroller/1?user=john GET很容易,因为框架正确地路由到每个函数。然而,当我使用POST时,它不起作用。我在同一个控制器中有2个POST函数。例如:C# mvcwebapi中带查询参数的投递路由,c#,asp.net-mvc,C#,Asp.net Mvc,在Web Apì项目中,我想使用以下内容: 职位/我的控制员/1 但也包括POST/mycontroller/1?user=john GET很容易,因为框架正确地路由到每个函数。然而,当我使用POST时,它不起作用。我在同一个控制器中有2个POST函数。例如: void Post(int id, string content) 及 我希望在调用POST/mycontroller/1?user=john时,框架将路由到POST(int-id,string-content,string-user)
void Post(int id, string content)
及
我希望在调用POST/mycontroller/1?user=john时,框架将路由到POST(int-id,string-content,string-user)
我知道我可以使用绑定模型,做一个模型类和一个唯一的POST函数,但这是一个混乱,因为我有很多函数,我希望能够使用查询参数来路由正确的函数。
可能吗?尝试用[FromBody]和[FromUri]属性声明参数,如下所示:
public string Post(int id, [FromBody]string content, [FromUri] string user)
{
return "content = " + content + "user = " + user;
}
有了上面的代码,我就可以打电话了
/Test/1?用户=Ryan
请求主体
“测试机构”
结果是:
“内容=测试主体用户=Ryan”
希望这有帮助
public string Post(int id, [FromBody]string content, [FromUri] string user)
{
return "content = " + content + "user = " + user;
}