Search code examples
asp.net-core

Why has Newtonsoft [JsonIgnore] stopped working in asp core mvc


Why is my property with a [JsonIgnore] showing in the returned result?

In fact why are all my Newtonsoft attributes not working?

using Newtonsoft.Json;
using System;

namespace Namespace
{
    public class Model
    {
        [JsonIgnore]
        public string IgnoredProperty { get; set; } <-- IS NOT IGNORED
    }
}

Replacing it with IgnoreDataMember or ScriptIgnore doesn't work?


Solution

  • It's because asp net core has, since Preview 6, changed which default serializer it uses.

    Either change it back by adding .AddNewtonsoftJson() to your MVC options.

    services.AddControllers().AddNewtonsoftJson()
    

    Or go to your models and start using the System.Text.Json serializer instead.

     using System.Text.Json.Serialization;
     // using Newtonsoft.Json; <- instead of this