简体   繁体   中英

Web Api returning empty response

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM