我们如何支持ajax帖子?
这是服务器代码:
[RoutePrefix("api/Dashboard")]
public class PatientDashboardController : ApiController
{
[Route("UpdatePatientById")]
[HttpPost]
public IHttpActionResult UpdatePatientById(int? pk, string name, object value )
{
return Ok(name);
}
}
这是我发布到服务器的内容
请求网址:http://localhost/mydomain/api/Dashboard/UpdatePatientById
请求方法:POST
名:性别
值:1
PK:1093
我在前端使用x-editable插件,它会自动执行ajax发布。我不认为邮政网址有任何问题。
这给我的错误:
"未找到与请求URI匹配的HTTP资源' http://example.com/mydomain/api/Dashboard/UpdatePatientById'。"
MessageDetail:
"No action was found on the controller 'Dashboard' that matches the request."
答案 0 :(得分:3)
Web API只能从正文中接收一个参数,因此您必须将其指定为聚合这些字段的类型。
class PatientParameters
{
public int? Pk { get; set; }
public string Name { get; set; }
public object Value { get; set; }
}
并通过:
public IHttpActionResult UpdatePatientById([FromBody] PatientParameters parameters) { }