ASP.NET Web API Custom JsonConverter is never called(永远不会调用 ASP.NET Web API 自定义 JsonConverter)
问题描述
这是我的情况.我正在 WebForms 应用程序中实现一个 WEB API.我有一堆动态类,它们本质上是字典,需要使用自定义 JSON 序列化格式化程序才能正常工作(因为默认转换器只是显示一堆键值对).
So here's my situation. I'm implementing a WEB API in a WebForms app. I have a bunch of dynamic classes which are essentially dictionaries that need to use a custom JSON serialization formatter in order to work properly (because the default converter simply shows a mess of Key Value pairings).
所以首先我创建了一个自定义的 JSON 转换器:
So first I created a custom JSON converter:
/// <summary>
/// A class to convert entities to JSON
/// </summary>
public class EntityJsonConverter : JsonConverter
{
public override bool CanConvert(Type objectType)
{
return objectType.IsSubclassOf(typeof(Entity));
}
public override bool CanRead
{
get { return true; }
}
public override bool CanWrite
{
get { return true; }
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
// Details not important. This code is called and works perfectly.
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
// Details not important. This code is *never* called for some reason.
}
}
定义好之后,我将其插入到全局 JSON 媒体类型格式化程序中:
Once I have that defined I then insert it into the global JSON media type formatter:
// Add a custom converter for Entities.
foreach (var formatter in GlobalConfiguration.Configuration.Formatters)
{
var jsonFormatter = formatter as JsonMediaTypeFormatter;
if (jsonFormatter == null)
continue;
jsonFormatter.SerializerSettings.Converters.Add(new EntityJsonConverter());
}
最后,我的测试 API(将来会添加更多,我现在只是尝试测试系统,Contact"继承自Entity"):
And lastly, my test API (there will be many more added in the future, I'm just trying to test out the system for now, "Contact" inherits from "Entity"):
public class ContactController : ApiController
{
public IEnumerable<Contact> Get()
{
// Details not important. Works perfectly.
}
[HttpPost]
public bool Update(Contact contact)
{
// Details not important. Contact is always "null".
}
}
所以这是我在调试时看到的:
So here's what I'm seeing when I debug:
网站调用get":
- Controller.Get 被调用.返回联系人列表.
- Converter.CanConvert 被调用用于枚举类型.返回 false.
- Converter.CanConvert 是为 Contact 类型调用的.返回 true.
- Converter.CanWrite 被调用.返回 true.
- Converter.WriteJson 被调用.将正确的 JSON 写入流
- 网站接收正确的 JSON 并能够将其用作对象.
网站调用更新":
- Converter.CanConvert 是为 Contact 类型调用的.返回 true.
- Controller.Update 被调用.联系人"参数为空".
我很困惑.我不明白为什么这在序列化时有效,但是在尝试反序列化时,整个过程似乎只是跳过了我的自定义转换器.有人知道我做错了什么吗?
I'm utterly perplexed. I don't understand why this works when serializing, but the entire process seems to just skip my custom converter when trying to deserialize. Anyone have any ideas what I'm doing wrong?
谢谢!
推荐答案
天啊.现在觉得自己很笨.
Ah geez. Now I feel dumb.
... 我没有在帖子数据中发送 JSON.我不小心发送了一堆乱七八糟的文字.哎呀……
... I wasn't sending JSON in the post data. I was accidentally sending a jumble of text. Whoops...
没关系!
这篇关于永远不会调用 ASP.NET Web API 自定义 JsonConverter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:永远不会调用 ASP.NET Web API 自定义 JsonConverter
基础教程推荐
- rabbitmq 的 REST API 2022-01-01
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01
- 如何激活MC67中的红灯 2022-01-01
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01
- MS Visual Studio .NET 的替代品 2022-01-01
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01
- 将 XML 转换为通用列表 2022-01-01
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30
- SSE 浮点算术是否可重现? 2022-01-01
- c# Math.Sqrt 实现 2022-01-01