在WebApi5.0以前调用接口传参数的方式有一些变化,也要从旧项目说起,一个小小的传参方式的变化,以前以URL方式接收参数现在改为从Body中接收Json格式的参数,报以下错:
{
"Message": "找不到与请求 URI“http://localhost/api/terminal/getpos”匹配的 HTTP 资源。",
"MessageDetail": "在控制器“terminal”上找不到与该请求匹配的操作。"
}
反复确认过方法名称和传的参数都能对上,不知何故,尝试着在接收参数的地方封装成Model加上一个[FromBody]居然都正常了。在新的webapi5.0及以上就没有这个问题,默认支持从url或者body中取参数。
示例代码如下:
[HttpPost]
public IHttpActionResult Login(string username,string uuid)
{
Entity.DTO.RespDto<int> respDto = new Entity.DTO.RespDto<int>();
respDto.code = "1";
respDto.msg = "验证通过";
return Json(respDto);
}
改为:
[HttpPost]
public IHttpActionResult Login([FromBody]ArgModel model)
{
Entity.DTO.RespDto<int> respDto = new Entity.DTO.RespDto<int>();
respDto.code = "1";
respDto.msg = "验证通过";
return Json(respDto);
}
public class ArgModel
{
public string username { get; set; }
public string uuid { get; set; }
}
推荐文章: