我有一個Put
操作。 Http請求值不能傳遞給第二個參數。Asp.Net核心Web Api錯誤? 「Put」無法獲得第二個參數的值?
[HttpPut("{id}")]
public async Task<IActionResult> Put(string id, [FromBody]DPost value)
{
Console.WriteLine($"id: {id} value.MyId: {value.MyId}"); // id: B161 value.MyId:
return Ok(value); // breakpoint here - id has value but value.MyId is null?
}
以下是該類的定義。
public class DPost
{
[Key]
public String MyId { get; set; }
}
下面顯示了原始Http請求。
PUT http://localhost:5001/api/D2/B161 HTTP/1.1
Host: localhost:5001
Connection: keep-alive
Content-Length: 52
Accept: application/json, text/plain, */*
Origin: http://localhost:8082
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36
Content-Type: application/json;charset=UTF-8
Referer: http://localhost:8082/
Accept-Encoding: gzip, deflate, sdch, br
Accept-Language: en-US,en;q=0.8
{"id":"B161","value":{"myId":"B161"}}
及其響應是
HTTP/1.1 200 OK
Date: Thu, 01 Dec 2016 18:29:02 GMT
Content-Type: application/json; charset=utf-8
Server: Kestrel
Access-Control-Allow-Origin: *
Content-Length: 12
{"myId":null}
它是Asp.net核心的一個bug?我還嘗試將參數value
更改爲簡單類型int?
,並更改了Http請求並仍爲空。
身份識別碼可能應該是大小寫敏感的,就像在你的JSON對象,所以它應該是身份識別碼。 – PmanAce
嘗試將http請求更改爲「MyId」,它是一樣的。 – ca9163d9