我习惯了HTTP内容协商的流程,但有些内容似乎不合适:
我有一个.NET WebApi控制器,其中包含以下内容:
/// <summary>
/// Search for Records corresponding to the given criterias
/// </summary>
[AcceptVerbs("POST")]
public async Task<IHttpActionResult> SearchRecord([FromBody]SearchPeopleModel recordSearchModel)
{
var recordService = Context.Services.Get<IRecordService>();
var result = await recordService.SearchRecord(recordSearchModel);
return Jsonify(result);
}
使用Fiddler,请求内容如下:
POST http://localhost:43465/api/Record/SearchRecord HTTP/1.1
Host: localhost:43465
Connection: keep-alive
Content-Length: 72
Pragma: no-cache
Cache-Control: no-cache
Accept: application/json
Origin: (hidden)
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36
(KHTML, like Gecko) Chrome/60.0.3112.101 Safari/537.36
Content-Type: application/json
Referer: http://localhost:43465/
Accept-Encoding: gzip, deflate, br
Accept-Language: fr-FR,fr;q=0.8,en-US;q=0.6,en;q=0.4
Cookie: ASP.NET_SessionId=(hidden)
{"Any":"mat","Firstname":"","Lastname":"","Birthname":"","PatientId":""}
调试WebApi时,生成的recordSearchModel为NULL。 我试图使用正文内容和JSON.stringify / not,这似乎也不是问题。
我一定错过了什么,但......我看不到哪里!希望有人可以帮忙!
编辑:在这里请求searchPeopleModel类。我不会显示RecordService的实现,因为它与此处的问题无关。
public class SearchPeopleModel
{
public string Any {get; set;}
public string Firstname { get; set; }
public string Lastname { get; set; }
public string Birthname { get; set; }
public DateTime? BirthDate { get; set; }
public Guid? PatientId { get; set; }
}
我可能不清楚一些精度:当ASP.NET WEB API收到请求时,recordSearchModel为null,其余代码完全符合预期。
答案 0 :(得分:0)
我终于把它挖了出来。
出现参数
public Guid? PatientId { get; set; }
默认情况下,asp.net webapi无法对SearchPeopleModel进行反序列化。 要使这种行为起作用,我们必须发送:
{"Any":"mat","Firstname":"","Lastname":"","Birthname":""}
而不是:
{"Any":"mat","Firstname":"","Lastname":"","Birthname":"","PatientId":""}
原因: “”不能变成有效的null Guid。 “[some-valid-guid]”虽然有效。
希望它可以帮助别人。