Abdullah El-Menawy
Abdullah El-Menawy

Reputation: 388

Web API - Error in sample generation

I'm facing a strange problem in ASP.NET web api. I made a generic class to be the returned model for all web methods. This is the model's code :-

public class RequestResponse<T>
{
    public bool Sucess { get; set; }
    public string Message { get; set; }
    public T ReturnedData { get; set; }
    public List<T> ReturnedDataList { get; set; }
}

Whenever I try using it in HTTP method like this :

public RequestResponse<BillsModel> CreateBill([FromBody] BillsModel billToAdd, MainHelpers.UserType userType)

This is BillsModel class :

public class BillsModel
{
    public Guid Id { get; set; }
    public DateTime BillDate { get; set; }
    public long Number { get; set; }
    public Guid OrderId { get; set; }
    public int OrderType { get; set; }
    public Guid PlaceId { get; set; }
    public double TaxPercentage { get; set; }
    public double? DiscountPercentage { get; set; }
    public Guid CreatedBy { get; set; }
    public DateTime CreatedOn { get; set; }
    public Guid? ModifiedBy { get; set; }
    public DateTime? ModifiedOn { get; set; }
}

And then running the api to see the methods and choosing a method, I see the below :

{
    "Sucess" : true,
    "Message" : "sample string 2",
    "ReturnedData" : {
        "$id" : "2",
        "Id" : "14b479ec-c128-4916-8ed5-c0067c20fd9f",
        "BillDate" : "2016-05-20T21:57:32.530957+02:00",
        "Number" : 3,
        "OrderId" : "dd266c00-0167-49e8-a8f0-0996aca21490",
        "OrderType" : 5,
        "PlaceId" : "98059b6e-acfb-4c89-8c6a-72ef30cce4d6",
        "TaxPercentage" : 7.1,
        "DiscountPercentage" : 1.1,
        "CreatedBy" : "ba106082-44be-4a53-9d32-40bf4ee32bde",
        "CreatedOn" : "2016-05-20T21:57:32.530957+02:00",
        "ModifiedBy" : "3d164ffd-aa1c-40d4-a646-9d6ca91615db",
        "ModifiedOn" : "2016-05-20T21:57:32.530957+02:00"
    },
    "ReturnedDataList" : {
        "$id" : "3",
        "$values" : [{
                "$ref" : "2"
            }, {
                "$ref" : "2"
            }
        ]
    }
}

As you can see, it works fine when it's about an object but when it comes to a list, an unidentified object is shown "$ref". The same for "$id" and "$values".

Can you help me with that please ?

Upvotes: 0

Views: 82

Answers (2)

Nasreddine
Nasreddine

Reputation: 37798

You can disable this behavior by adding this at the end of your Application_Start() method Global.asax:

GlobalConfiguration
    .Configuration
    .Formatters
    .JsonFormatter
    .SerializerSettings
    .PreserveReferencesHandling = Newtonsoft.Json.PreserveReferencesHandling.None;

Upvotes: 1

dfdsfdsfsdf
dfdsfdsfsdf

Reputation: 663

I created the following web service and am using a generic type to return the packaged data and I'm not getting what you're getting. Can you run this on a separate project and see what JSON data is being returned?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

public class Account
{
    public int AccountID { get; set; }

}
public class BusinessAccount : Account
{
    public string BusinessAddress { get; set; }
}

public class Package<T>
{
    public T Data { get; set; }
}

/// <summary>
/// using fiddler, I made a request to http://localhost/api/account
/// and got
/// 
/// {"Data":{"BusinessAddress":"ABC","AccountID":123}}
/// 
/// without making any configuration.
///  
/// 
/// </summary>  
namespace test
{
    public class AccountController : ApiController
    {
        // GET api/<controller>
        public Package<BusinessAccount> Get()
        {
            return new Package<BusinessAccount>()
            {
                Data = new BusinessAccount()
                {
                    AccountID = 123,
                    BusinessAddress = "ABC"
                }
            };
        }
    }
}

Upvotes: 0

Related Questions