我有一个具有以下方法的 ApiController:
public void Post(dynamic data)
{
Type actualType = data.GetType(); // returns JObject
}
当我进行 AJAX 调用时,将 JSON 数据发送到 Controller ,“数据”参数的实际类型是 JObject(来自 Newtonsoft.Json 库)。 WebAPI 的哪一部分负责将此请求中的数据绑定(bind)到 JObject?它是模型 Binder 吗?如果有,是哪一个? (有几个内置的,例如'MutableObjectModelBinder','TypeMatchModelBinder'等)
请您参考如下方法:
它是 JsonMediaTypeFormatter。 ASP.NET Web API 默认的 JSON Formatter 是 JSON.NET。这就是你的动态对象被转换为 JObject 的原因。不过,您可以将默认格式化程序切换为 DataContractJsonSerializer。
查看 this article .
JSON Media-Type Formatter
JSON formatting is provided by the JsonMediaTypeFormatter class. By default, JsonMediaTypeFormatter uses the Json.NET library to perform serialization. Json.NET is a third-party open source project.
If you prefer, you can configure the JsonMediaTypeFormatter class to use the DataContractJsonSerializer instead of Json.NET. To do so, set the UseDataContractJsonSerializer property to true:
var json = GlobalConfiguration.Configuration.Formatters.JsonFormatter; json.UseDataContractJsonSerializer = true;
您可以从 WebApiConfig 更改您的 JSON 格式化程序配置
