Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ScriptIgnore ASP 6

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 ?

like image 942
Léo Moro Avatar asked May 12 '16 13:05

Léo Moro


3 Answers

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

like image 116
CrazyPyro Avatar answered Nov 12 '22 19:11

CrazyPyro


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.

like image 37
James P Avatar answered Nov 12 '22 17:11

James P


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;}

}

like image 22
Міша Шмигельський Avatar answered Nov 12 '22 18:11

Міша Шмигельський