我有一个Put
行动。 Http请求值无法传递给第二个参数。
[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核心的错误吗?我还尝试将参数value
更改为简单类型int?
并更改了Http请求,但它仍然为空。
答案 0 :(得分:1)
错误回答:value.Id
实际上已分配给第一个值{id:"first", value:{id: "second"}}
。它应该得到第二个&#34;。这是一个错误吗?
我通过将班级DPost
更改为:
public class DPost
{
[Key]
public String Id { get; set; }
}
为什么呢?是否需要不敏感地匹配第一个参数大小写?
答案 1 :(得分:0)
像这样添加var proto = {
name: "Name on prototype"
};
var obj = Object.create(proto, {
name: {
set(value) {
}
}
});
console.log(obj.hasOwnProperty("name")); // true
console.log(obj.name); // undefined
:
[FromBody]
它告诉引擎从请求正文中检索对象。
答案 2 :(得分:0)
将MyId的大小写更改为以下内容:
public class DPost
{
[Key]
public String myId { get; set; }
}
匹配json对象:
{"id":"B161","value":{"myId":"B161"}}
答案 3 :(得分:0)
您需要以这种方式放置参数:
{"id":"B161","myId":"B161"}