的ASP.NET Web API定制JsonConverter不会被调用 [英] ASP.NET Web API Custom JsonConverter is never called

查看:128
本文介绍了的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(将有更多的在未来加入,我只是想测试一下系统就目前来看,联系方式继承实体):

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:

网站调用得到:

  1. Controller.Get被调用。联系人返回列表。
  2. Converter.CanConvert被调用枚举类型。返回FALSE。
  3. Converter.CanConvert被调用联系人类型。返回true。
  4. Converter.CanWrite被调用。返回true。
  5. Converter.WriteJson被调用。写正确的JSON向流
  6. 网站接收适当JSON和能够使用它作为一个对象。

网站调用更新:

  1. Converter.CanConvert被调用联系人类型。返回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天全站免登陆