Can I pass an interface based object to an MVC 4 WebApi POST?

前端 未结 4 1238
半阙折子戏
半阙折子戏 2020-12-30 03:24

I want to have an API as such:

public class RelayController : ApiController
{
    // POST api/values
    public void Post([FromBody]IDataRelayPackage package         


        
4条回答
  •  我在风中等你
    2020-12-30 03:44

    You are trying to deserialise to an interface. The serialiser won't know what type to instantiate unless it is told.

    Take a look at TypeNameHandling option Posting a collection of subclasses

    Or look at creating a custom JsonConverter. Take a look at this question How to implement custom JsonConverter in JSON.NET to deserialize a List of base class objects?

提交回复
热议问题