Issue with MVC API and ContentType or Accept as text/xml RRS feed

  • Question

  • Came across something strange when the tester of my API interface said the data wasn't being populated into his test object.     He was calling the api with  .Accept = "text/xml"

    object was filled correctly apart from 3 values that were missing.

    If he removed the type, then the json that was returned was correct.

    Transpires that the problem is in the object that was being used to build the list there were a couple of properties that would return a private value based on another -   example.

    public string OwnerKey { get { return IsOrganizer ? key : string.Empty; } }

    this, when returned with JSON,  correctly showing the value in the key field, if the IsOrganizer value was true.    when the accept type was set to text/xml  then the field was empty.

    This was not the only object that had this problem, and all fields where a similar method was employed caused the same issue.   Fine in JSON, problem with XML.

    Looks like its an issue with the framework not accessing the object correctly when XML serialising,  or  is there some attribute that I need to attach to the method. I looked at XmlInclude but thats only for classes etc

    Any suggestions?



    Tuesday, November 27, 2012 8:44 AM

All replies

  • I don't know how that API works, but XML Serialization requires public read/write properties. Try adding a dummy setter to the property:

    public string OwnerKey 
        get { return IsOrganizer ? key : string.Empty; }
        set {}

    John Saunders
    WCF is Web Services. They are not two separate things.
    Use WCF for All New Web Service Development, instead of legacy ASMX or obsolete WSE
    Use File->New Project to create Web Service Projects

    Wednesday, November 28, 2012 4:30 PM