JSON 传递给 ApiController 不反序列化字符串值
JSON passed to ApiController doesn't deserialize string values
我正在尝试将 JSON 数组传递给 ApiController,但字符串值未反序列化(它们被设置为空值)。奇怪的是我仍然得到正确数量的元素。
A 有一个 ApiController:
[RoutePrefix("api/language")]
public class LanguagePairApiController : ApiController
使用post方法:
// POST: api/language/create
[HttpPost]
[Route("create")]
public string Create([FromBody]LanguagePair[] languagePairs)
我正在发送 JSON 给它:
[
{"Key":"Test","Value":"Test","Version":"1.0"},
{"Key":"Areyousure","Value":"Are you sure?","Version":"1.0"},
{"Key":"File","Value":"File","Version":"1.0"}
]
这是 class 我正在尝试将其映射到:
public class LanguagePair
{
public string Key { get; set; }
public string Value { get; set; }
public string Version { get; set; }
}
但是字符串值作为空值通过:
我错过了什么?
编辑:我找到了一个答案并在下面post编辑了它。但我仍在寻找更好的答案...
我明白了。我需要用 DataContract 和 DataMember 属性装饰 class:
{
[DataContract]
public class LanguagePair
{
[DataMember]
public string Key { get; set; }
[DataMember]
public string Value { get; set; }
[DataMember]
public string Version { get; set; }
}
}
阅读Parameter Binding in ASP.NET Web API
您需要从操作中删除 [FromBody]
属性...
// POST: api/language/create
[HttpPost]
[Route("create")]
public string Create(LanguagePair[] languagePairs) { ... }
并且您可以保持 class 原来的苗条身材:
public class LanguagePair
{
public string Key { get; set; }
public string Value { get; set; }
public string Version { get; set; }
}
Using [FromBody]
To force Web API to read a simple type from the request body, add the
[FromBody]
attribute to the parameter:
public HttpResponseMessage Post([FromBody] string name) { ... }
In this example, Web API will use a media-type formatter to read the
value of name from the request body. Here is an example client
request.
POST http://localhost:5076/api/values HTTP/1.1
User-Agent: Fiddler
Host: localhost:5076
Content-Type: application/json
Content-Length: 7
"Alice"
When a parameter has [FromBody]
, Web API uses the Content-Type
header to select a formatter. In this example, the content type is
"application/json" and the request body is a raw JSON string (not a
JSON object).
我正在尝试将 JSON 数组传递给 ApiController,但字符串值未反序列化(它们被设置为空值)。奇怪的是我仍然得到正确数量的元素。
A 有一个 ApiController:
[RoutePrefix("api/language")]
public class LanguagePairApiController : ApiController
使用post方法:
// POST: api/language/create
[HttpPost]
[Route("create")]
public string Create([FromBody]LanguagePair[] languagePairs)
我正在发送 JSON 给它:
[
{"Key":"Test","Value":"Test","Version":"1.0"},
{"Key":"Areyousure","Value":"Are you sure?","Version":"1.0"},
{"Key":"File","Value":"File","Version":"1.0"}
]
这是 class 我正在尝试将其映射到:
public class LanguagePair
{
public string Key { get; set; }
public string Value { get; set; }
public string Version { get; set; }
}
但是字符串值作为空值通过:
我错过了什么?
编辑:我找到了一个答案并在下面post编辑了它。但我仍在寻找更好的答案...
我明白了。我需要用 DataContract 和 DataMember 属性装饰 class:
{
[DataContract]
public class LanguagePair
{
[DataMember]
public string Key { get; set; }
[DataMember]
public string Value { get; set; }
[DataMember]
public string Version { get; set; }
}
}
阅读Parameter Binding in ASP.NET Web API
您需要从操作中删除 [FromBody]
属性...
// POST: api/language/create
[HttpPost]
[Route("create")]
public string Create(LanguagePair[] languagePairs) { ... }
并且您可以保持 class 原来的苗条身材:
public class LanguagePair
{
public string Key { get; set; }
public string Value { get; set; }
public string Version { get; set; }
}
Using [FromBody]
To force Web API to read a simple type from the request body, add the
[FromBody]
attribute to the parameter:public HttpResponseMessage Post([FromBody] string name) { ... }
In this example, Web API will use a media-type formatter to read the value of name from the request body. Here is an example client request.
POST http://localhost:5076/api/values HTTP/1.1 User-Agent: Fiddler Host: localhost:5076 Content-Type: application/json Content-Length: 7 "Alice"
When a parameter has
[FromBody]
, Web API uses the Content-Type header to select a formatter. In this example, the content type is "application/json" and the request body is a raw JSON string (not a JSON object).