I'm trying to ignore a field when I am converting my object into a json string. I saw on Internet that I have to use [ScriptIgnore] on my field. But I can't find any way to import the System.Web.Script...
i'm using this :
"dependencies": {
"AutoMapper": "4.2.1",
"EntityFramework.Commands": "7.0.0-rc1-final",
"EntityFramework.Core": "7.0.0-rc1-final",
"EntityFramework.MicrosoftSqlServer": "7.0.0-rc1-final",
"Microsoft.ApplicationInsights.AspNet": "1.0.0-rc1",
"Microsoft.AspNet.IISPlatformHandler": "1.0.0-rc1-final",
"Microsoft.AspNet.Mvc": "6.0.0-rc1-final",
"Microsoft.AspNet.Server.Kestrel": "1.0.0-rc1-final",
"Microsoft.AspNet.StaticFiles": "1.0.0-rc1-final",
"Microsoft.Extensions.Configuration.FileProviderExtensions": "1.0.0-rc1-final",
"Microsoft.Extensions.Configuration.Json": "1.0.0-rc1-final",
"Microsoft.Extensions.Logging": "1.0.0-rc1-final",
"Microsoft.Extensions.Logging.Console": "1.0.0-rc1-final",
"Microsoft.Extensions.Logging.Debug": "1.0.0-rc1-final"
},
Is this because this is incompatible due to the MVC 6 ?
Without resorting to System.Web
, which I think is good to avoid:
Since MVC has switched formatters, you should now use [JsonIgnore]
(from Newtonsoft.Json
) instead of [ScriptIgnore]
.
Oh and if you're doing this to prevent reference loops, do this: https://stackoverflow.com/a/36633265/268066
Right click on 'References' in solution explorer then select 'Add References..' - you will need to add System.Web.Extensions
from Assemblies - DNX 4.5.1 to your solution.
Note you may then get compiler error about DNX Core 5.0
which is a lightweight version of the framework optimized for cloud. If you do not need to support it just remove from "frameworks" in project.json
.
// comment out or delete.
"dnxcore50": { }
Just for future reference if you look up the relevant MSDN documentation it shows which Assembly you need to include.
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
public class Page
{
public int Id { get; set; }
public int Number { get; set; }
public int? BookId { get; set; }
[JsonProperty(ReferenceLoopHandling = ReferenceLoopHandling.Ignore)]
public Book Book{ get; set; }
}
////////////////
public class Book {
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection Pages { get;set;}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With