3

I'm Using Web Api with Entity Framework, I have a controller named as Gift, when client GET's the API I'm getting all Gifts from DB by using entity framework and and returning it as JSON

Here is my controller function

    public List<Gift> Get()
    {
        return GiftService.GetIncludeAndActive();
    }

Here is Service Function

public List<Gift> GetIncludeAndActive()
{
    return dbSet.Include("GiftCategory").Where(x => x.Status == Model.Enums.GiftStatus.Active).OrderByDescending(x => x.Featured).ThenBy(x => x.Price).ToList();
}

Here is Gift Model

public class Gift
    {
        public Gift()
        {
            this.CartItems = new List<CartItem>();
        }
        public int ID { get; set; }
        public string GiftName { get; set; }
        public string Image { get; set; }
        public int Stock { get; set; }
        public int Price { get; set; }
        public string Description { get; set; }
        public string Status { get; set; }
        public string GiftCode { get; set; }
        public int GiftCategoryID { get; set; }
        public Nullable<DateTime> CreatedAt { get; set; }
        public Nullable<DateTime> UpdatedAt { get; set; }
        public bool Featured { get; set; }
        public GiftCategory GiftCategory { get; set; }
        public ICollection<CartItem> CartItems { get; set; }

    }

[

Here is the JSON Output

{
        "$id": "1",
        "ID": 14,
        "GiftName": "Sinbo SVC-3438 1600 Watt Elektrikli Süpürge",
        "Image": "http://placehold.it/400x288.jpg/A6A",
        "Stock": 70,
        "Price": 260,
        "Description": "Az yer kaplayan Kompakt dizayn, Max. 1600W, 5m kablo uzunluğu, 360 derece dönüş açılı ön tekerlek, Dar ve ulaşılması zor yerler için 2 si 1 arada aparat ",
        "Status": "Active",
        "GiftCode": "BDRS-498",
        "GiftCategoryID": 4,
        "CreatedAt": "2016-12-19T12:59:01.31",
        "UpdatedAt": "2016-12-19T12:59:10.54",
        "Featured": false,
        "GiftCategory": {
          "$id": "2",
          "ID": 4,
          "Name": "Kişisel Bakım",
          "CountryID": 1,
          "Country": null,
          "Gifts": [
            {
              "$ref": "1"
            },
            {
              "$id": "3",
              "ID": 11,
              "GiftName": "Kişisel Bakım 2 - LG 43LH590V 43\"108 Ekran Full HD",
              "Image": "http://placehold.it/400x288.jpg/AEA",
              "Stock": 50,
              "Price": 3600,
              "Description": "Triple XD Engine teknolojisiyle güçlendirilen, Active Noise Reduction ve Real Cinema 24p özellikleriyle zenginleştirilen 1080p çözünürlüğündeki LED aydınlatmalı 55 inçlik ekrana sahip model, size ideal sinema keyfi sunuyor. Aynı zamanda sahip olduğu Color Prime, Dynamic Clear White ve Dynamic Colour Enhancer özellikleri sayesinde kusursuz bir görüntüyle evlerinizde yerini alıyor.",
              "Status": "Active",
              "GiftCode": "BDRS-495",
              "GiftCategoryID": 4,
              "CreatedAt": "2016-12-19T12:59:01.31",
              "UpdatedAt": "2016-12-19T12:59:10.54",
              "Featured": false,
              "GiftCategory": {
                "$ref": "2"
              },
              "CartItems": []
            },
            {
              "$id": "4",
              "ID": 8,
              "GiftName": "Kişisel Bakım 1 - Apple iPhone 6S 16 GB",
              "Image": "http://placehold.it/400x288.jpg/CCC",
              "Stock": 100,
              "Price": 5000,
              "Description": "Teknolojinin sunduğu tüm imkanlardan yararlanılarak tasarlanan Apple iPhone 6S , hem işlevsel hem de görsel açıdan ayrıcalıklı bir akıllı telefona sahip olmanıza olanak tanıyor.",
              "Status": "Active",
              "GiftCode": "BDRS-492",
              "GiftCategoryID": 4,
              "CreatedAt": "2016-12-19T12:59:01.31",
              "UpdatedAt": "2016-12-19T12:59:10.54",
              "Featured": false,
              "GiftCategory": {
                "$ref": "2"
              },
              "CartItems": []
            }
          ]
        },
        "CartItems": []
      },
      {
        "$id": "5",
        "ID": 13,
        "GiftName": "Beyaz Eşyalar 2 - Philips Marathon Ultimate FC9919/07 A Sınıfı Toz Torbasız Elektrikli Süpürge",
        "Image": "http://placehold.it/400x288.jpg/A1A",
        "Stock": 70,
        "Price": 900,
        "Description": "Yeni Philips Marathon Ultimate torbasız elektrikli süpürge, üstün temizlik performansı sunar. PowerCyclone 7, havayla tozu olağanüstü bir performansla ayırır. TriActiveMax başlık ise tüm zeminlerde mükemmel performans gösterir.",
        "Status": "Active",
        "GiftCode": "BDRS-497",
        "GiftCategoryID": 2,
        "CreatedAt": "2016-12-19T12:59:01.31",
        "UpdatedAt": "2016-12-19T12:59:10.54",
        "Featured": false,
        "GiftCategory": {
          "$id": "6",
          "ID": 2,
          "Name": "Beyaz Eşyalar",
          "CountryID": 1,
          "Country": null,
          "Gifts": [
            {
              "$ref": "5"
            },
            {
              "$id": "7",
              "ID": 6,
              "GiftName": "Beyaz Eşyalar 1 - Samsung Galaxy S7 Edge",
              "Image": "http://placehold.it/400x288.jpg/EEE",
              "Stock": 100,
              "Price": 6000,
              "Description": "Teknolojik yeniliklerin nereye kadar ulaşacağını kestiremeyen kullanıcılar, Samsung’un geliştirdiği ve bünyesinde barındırdığı yenilikçi özelliklerle öne çıkan telefonlarla şaşırmaya devam ediyor. Galaxy serisi içerisindeki en agresif atılımları üzerinde bulunduran Samsung Galaxy S7 Edge modelleri;alüminyum çerçeveleri, güçlü donanımları, benzersiz kameraları, suya ve toza karşı dayanıklılıkları ve sanal gerçeklik aygıtlarına doğrudan bağlanabilmeleriyle benzersiz hale geliyorlar.",
              "Status": "Active",
              "GiftCode": "BDRS-490",
              "GiftCategoryID": 2,
              "CreatedAt": "2016-12-19T12:59:01.31",
              "UpdatedAt": "2016-12-19T12:59:10.54",
              "Featured": false,
              "GiftCategory": {
                "$ref": "6"
              },
              "CartItems": []
            }
          ]
        },
        "CartItems": []
      },
      {
        "$id": "8",
        "ID": 7,
        "GiftName": "Elektronik 1 - Samsung Galaxy J7",
        "Image": "http://placehold.it/400x288.jpg/DDD",
        "Stock": 1000,
        "Price": 2500,
        "Description": "eknolojik yenilikleri yakından takip eden herkesin kullandığı akıllı cep telefonları, gelişmiş donanım ve yazılım özelliklerinin yanı sıra üzerlerinde bulunan kameralarla daha da kullanışlı hale geliyor. Dünya çapında milyonlarca kullanıcıya ulaşan ve akıllı cep telefonu sektörüne yön veren başlıca markalardan biri olan Samsung, Galaxy serisi içerisine konumlandırdığı Samsung Galaxy J7 modeliyle kendinden söz ettiriyor. ",
        "Status": "Active",
        "GiftCode": "BDRS-491",
        "GiftCategoryID": 3,
        "CreatedAt": "2016-12-19T12:59:01.31",
        "UpdatedAt": "2016-12-19T12:59:10.54",
        "Featured": false,
        "GiftCategory": {
          "$id": "9",
          "ID": 3,
          "Name": "Elektronik ",
          "CountryID": 1,
          "Country": null,
          "Gifts": [
            {
              "$ref": "8"
            },
            {
              "$id": "10",
              "ID": 12,
              "GiftName": "Elektronik  2 - Samsung 40JU6070 40\" 102 Ekran Ultra HD",
              "Image": "http://placehold.it/400x288.jpg/A6A",
              "Stock": 62,
              "Price": 3200,
              "Description": "TV standında kullanılabilen model, A enerji verimlilik sınıfında yer alıyor. Samsung 40JU6070 fiyatı kullanıcı açısından tercih edilir olmasında önemli bir rol oynuyor.4K teknolojisiyle yeni tanışacak olanların rahatlıkla yönelebilecekleri model, zengin giriş - çıkış noktalarıyla da cazip seçenekler arasında yer alıyor. Slim yapısı, pratik kullanımı ve gerek donanım gerekse yazılım anlamında Samsung’un elektronik alandaki deneyiminin bir eseri olarak nitelenebilecek akıllı televizyon, salon ya da oturma odalarınızın en sevilen eşyalarından biri oluyor.",
              "Status": "Active",
              "GiftCode": "BDRS-496",
              "GiftCategoryID": 3,
              "CreatedAt": "2016-12-19T12:59:01.31",
              "UpdatedAt": "2016-12-19T12:59:10.54",
              "Featured": false,
              "GiftCategory": {
                "$ref": "9"
              },
              "CartItems": []
            }
          ]
        },
        "CartItems": []
      },
      {
        "$id": "11",
        "ID": 9,
        "GiftName": "Küçük Ev Aletleri 1 - General Mobile 4G Android One",
        "Image": "http://placehold.it/400x288.jpg/BBB",
        "Stock": 150,
        "Price": 2600,
        "Description": "General Mobile 4G , diğer akıllı telefon modellerinden farklı olarak Google iş birliği ile geliştirilen Türkiye'deki ilk Android One cihaz olma özelliğine sahip ve bu yüzden benzeri bulunmayan eşsiz bir ürün.",
        "Status": "Active",
        "GiftCode": "BDRS-493",
        "GiftCategoryID": 5,
        "CreatedAt": "2016-12-19T12:59:01.31",
        "UpdatedAt": "2016-12-19T12:59:10.54",
        "Featured": false,
        "GiftCategory": {
          "$id": "12",
          "ID": 5,
          "Name": "Küçük Ev Aletleri",
          "CountryID": 1,
          "Country": null,
          "Gifts": [
            {
              "$ref": "11"
            }
          ]
        },
        "CartItems": []
      },
      {
        "$id": "13",
        "ID": 10,
        "GiftName": "Elektrikli Ev Aletleri 2 - Samsung 40J5070 40\" 102 Ekran Full HD",
        "Image": "http://placehold.it/400x288.jpg/AAA",
        "Stock": 50,
        "Price": 3000,
        "Description": "\r\nMarkaların ortaya koydukları televizyon modelleri, farklı donanım ve yazılım özellikleriyle öne çıkıyor. Gelişmiş 3D yetenekleri ve 4K çözünürlükleriyle sunulan bazı ürünler, sahip oldukları aygıttan çok sayıda özellik bekleyenlerin gereksinimlerini kolayca karşılıyor.",
        "Status": "Active",
        "GiftCode": "BDRS-494",
        "GiftCategoryID": 1,
        "CreatedAt": "2016-12-19T12:59:01.31",
        "UpdatedAt": "2016-12-19T12:59:10.54",
        "Featured": false,
        "GiftCategory": {
          "$id": "14",
          "ID": 1,
          "Name": "Elektrikli Ev Aletleri",
          "CountryID": 1,
          "Country": null,
          "Gifts": [
            {
              "$ref": "13"
            },
            {
              "$id": "15",
              "ID": 5,
              "GiftName": "Elektrikli Ev Aletleri 1 - Iphone 7 Plus 128GB",
              "Image": "http://placehold.it/400x288.jpg/FFF",
              "Stock": 200,
              "Price": 7500,
              "Description": "Apple markası iPhone serisi altında sunduğu cihazların arasına geçtiğimiz yıllarda Plus serisini de eklemişti. iPhone 7 cihazının yanı sıra sunulan iPhone 7 Plus , yüksek özellikleri ve normal iPhone’a göre büyük ekranı ile büyük ekran beklentisi olanların ihtiyacını karşılamayı hedefliyor.",
              "Status": "Active",
              "GiftCode": "BDRS-489",
              "GiftCategoryID": 1,
              "CreatedAt": "2016-12-19T12:59:01.31",
              "UpdatedAt": "2016-12-19T12:59:10.54",
              "Featured": false,
              "GiftCategory": {
                "$ref": "14"
              },
              "CartItems": []
            }
          ]
        },
        "CartItems": []
      },
      {
        "$ref": "10"
      },
      {
        "$ref": "3"
      },
      {
        "$ref": "4"
      },
      {
        "$ref": "7"
      },
      {
        "$ref": "15"
      }
    ]

As you can see there is "$ref":"13", "$ref":"14" etc... I don't want to show that $refs at the bottom of the json, I don't want $refs. I want a regular JSON file

And here is my WebApiConfig.cs

     public static void Register(HttpConfiguration config)
        {

            config.SuppressDefaultHostAuthentication();
            config.Filters.Add(new HostAuthenticationFilter(OAuthDefaults.AuthenticationType));
            config.Formatters.JsonFormatter.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
            config.Formatters.JsonFormatter.SerializerSettings.PreserveReferencesHandling = Newtonsoft.Json.PreserveReferencesHandling.None;
.....

I can fix this problem by using a very mystic way; I'm adding a new blank line to web.config file and saving it, then I'm calling the API again and wholaa it is OK as I want.

But after 1-2 hours starting to send data with $ref until I add a new blank line to web.config file.

fobus
  • 1,938
  • 8
  • 29
  • 48
  • What do you want for `GiftCategory.Gifts[]`? From the JSON it's apparent that it's a back-reference to all the `Gift` objects that refer to it. – dbc Dec 22 '16 at 18:06

2 Answers2

9

Json.net adds "$id" and "$ref" to serialized JSON to handle circular references. Your Gift object contains GiftCategory that in its turn contains list of Gift objects.

Reference handling is controlled by PreserveReferencesHandling serializer setting, however setting SerializerSettings.PreserveReferencesHandling to PreserveReferencesHandling.None will not work for you in case of having circular references, because it will cause loosing of the data. Json.net is smart enough by not giving you shoot in own leg.

You could try to set ReferenceLoopHandling to ReferenceLoopHandling.Ignore as described in this great answer, however due to specific order of serialization, the Gift object will be serialized at first reference place. So some of them will get into Gifts array, and some of them will get into GiftCategory. See this question for more details.

So the possible fixes are:

  1. At first, as suggested here consider not serializing EF entities.

  2. If navigation property GiftCategory.Gifts isn't actually required by business logic of your application, you could delete it from the model entity. Then you will not have circular references anymore and serialized JSON will not have "$ref" and "$id" fields.

Community
  • 1
  • 1
CodeFuller
  • 30,317
  • 3
  • 63
  • 79
0

I solved the problem this way;

You have to write your app.config:

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        config.Formatters.JsonFormatter.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
    }
}

Result:

{
    "Id": 1,
    "Name": "Hasan Batuhan",
    "Surname": "Kurt",
    "Email": "mail@mail.com,
    "PhoneNumber": "09999999999",
    "Orders": [{
        "Id": 1,
        "BasketId": 1,
        "OrderDate": "2015-11-07T00:00:00+03:00"
    }]
}

Or;

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        config.Formatters.JsonFormatter.SerializerSettings.PreserveReferencesHandling = Newtonsoft.Json.PreserveReferencesHandling.All;
    }
}

result:

{
    "$id": "1",
    "Id": 1,
    "Name": "Hasan Batuhan",
    "Surname": "Kurt",
    "Email": "mail@mail.com",
    "PhoneNumber": "09999999999",
    "Orders": {
        "$id": "2",
        "$values": [{
            "$id": "3",
            "Id": 1,
            "BasketId": 1,
            "OrderDate": "2015-11-07T00:00:00+03:00",
            "Customer": {
                "$ref": "1"
            }
        }]
    }
}