永远不会调用 ASP.NET Web API 自定义 JsonConverter [英] ASP.NET Web API Custom JsonConverter is never called

查看:23
本文介绍了永远不会调用 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":

  1. Controller.Get 被调用.返回联系人列表.
  2. Converter.CanConvert 被调用用于枚举类型.返回 false.
  3. Converter.CanConvert 是为 Contact 类型调用的.返回 true.
  4. Converter.CanWrite 被调用.返回 true.
  5. Converter.WriteJson 被调用.将正确的 JSON 写入流
  6. 网站接收正确的 JSON 并能够将其用作对象.

网站调用更新":

  1. Converter.CanConvert 是为 Contact 类型调用的.返回 true.
  2. 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆