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

3927 views asp.net-core
4

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?

answered question

1 Answer

7

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

posted this

Have an answer?

JD

Please login first before posting an answer.