0

I am trying to return below model from my api method but it is returning empty reponse.

[DataContract]
    public class MasterData
    {
        public IEnumerable<PROFILE> lstProfile { get; set; }
        public IEnumerable<COMPETENCE> lstCOMPETENCE { get; set; }
        public IEnumerable<TB> lstTB { get; set; }

        public MasterData() { }

    }

Response :

<MasterData xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/WB.Q.Models"/>

When I am debugging web api method I can see the response properly formed at return statement,but I am not getting it as response

Action

[HttpGet]
[Route("api/Common/AllMasterData")]
public MasterData GetAllMasterData()
{
    MasterData mstrData=new MasterData();
    mstrData = Helper.GetAllMasterData();

    return mstrData;
}

Helper method

    internal static MasterData GetAllMasterData()
    {
        MasterData masterData = new MasterData();

        DataSet ds = DBHelper.GetData("aaa", null);
        List<COMPETENCE> lstCompetence = new List<COMPETENCE>();
        if (ds != null && ds.Tables.Count > 0)
        {
            foreach (DataRow drow in ds.Tables[0].Rows)
            {
                        Adding in list
            }
        }
        masterData.lstCOMPETENCE = lstCompetence;

        ds = DBHelper.GetData("bbb", null);
        List<PROFILE> lstPROFILE = new List<PROFILE>();
        if (ds != null && ds.Tables.Count > 0)
        {
            foreach (DataRow drow in ds.Tables[0].Rows)
            {
                                   Adding in list
            }
        }
        masterData.lstProfile = lstPROFILE;

        ds = DBHelper.GetData("zzz", null);
        List<TBSite> lstTBSite = new List<TBSite>();
        if (ds != null && ds.Tables.Count > 0)
        {
            foreach (DataRow drow in ds.Tables[0].Rows)
            {
               Adding in list
            }
        }
        masterData.lstTBSite = lstTBSite;

        return masterData;
    }

If I remove DataContract from Modal, I am getting below error:

The 'ObjectContent`1' type failed to serialize the response body for content type 'application/xml; charset=utf-8'.

Is it mandatory to use DataContract and DataMember if we have collection of object as a property?

1
  • What is the action? Commented Sep 7, 2018 at 18:44

1 Answer 1

2

If you are using DataContract like that then I believe you also need to mark everything you want in the response with DataMember as well:

[DataContract]
public class MasterData
{
    [DataMember]
    public IEnumerable<PROFILE> lstProfile { get; set; }

    [DataMember]
    public IEnumerable<COMPETENCE> lstCOMPETENCE { get; set; }

    [DataMember]
    public IEnumerable<TB> lstTB { get; set; }

    public MasterData() { }

}

I've made that mistake myself... like half a dozen times :)

Alternatively, you can just omit the DataContract attribute and everything should still serialize just fine.

Sign up to request clarification or add additional context in comments.

6 Comments

I don't think do we need to specify datacontract and datamember for webapi
@JEMI - I think you are right, but if you do specify DataContract, then I believe you have to use DataMember as well.
Do i need to add DataMember to every class(Modal) like TB,Profile?
@F11 I would suggest you to remove the DataContract from your class , Web API will internally take care of.
@F11 - If those classes are also marked with DataContract, then yes. Alternatively, you can just omit the DataContract attribute alltogether and everything should still serialize nicely as mentioned by JEMI.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.