“Type not expected”, using DataContractSerializer - but it's just a simple class, no funny stuff?

前端 未结 7 1559
慢半拍i
慢半拍i 2020-11-30 11:10

I\'m refactoring my XML-serialization, and figured I\'d try the DataContractSerializer. Everything runs smoothly, until it needs to serialize this class:

         


        
7条回答
  •  天命终不由人
    2020-11-30 11:16

    The problem for me was that I was returning the Interface (IIndividual) from my WebAPI controller. When I changed that return type to the Class (Individual) type, this error went away.

    Not working:

        [HttpGet]
        [Route("api/v1/Individual/Get/{id}")]
        public IIndividual Get([FromUri]int id)
        {
            return _individualService.Get(id);
        }
    

    Working:

        [HttpGet]
        [Route("api/v1/Individual/Get/{id}")]
        public Individual Get([FromUri]int id)
        {
            IIndividual individual = _individualService.Get(id);
            return individual as Individual;
        }
    

提交回复
热议问题