这是一个棘手的问题。我有一个从JSON绑定模型的问题。我试图解决多态的记录提供了它将解决的记录类型(我希望能够在将来添加多种记录类型)。我试图在调用端点时使用following example来解析我的模型,但是此示例仅适用于MVC而不适用于Web API应用程序。复杂抽象对象的WebAPI自定义模型绑定
我试图用IModelBinder和BindModel(HttpActionContext actionContext,ModelBindingContext bindingContext)编写它。但是我无法在System.Web.Http命名空间中找到等效的ModelMetadataProviders。
欣赏任何人都可以给的帮助。
我有一个Web API 2应用程序,它具有以下对象结构。
public abstract class ResourceRecord
{
public abstract string Type { get; }
}
public class ARecord : ResourceRecord
{
public override string Type
{
get { return "A"; }
}
public string AVal { get; set; }
}
public class BRecord : ResourceRecord
{
public override string Type
{
get { return "B"; }
}
public string BVal { get; set; }
}
public class RecordCollection
{
public string Id { get; set; }
public string Name { get; set; }
public List<ResourceRecord> Records { get; }
public RecordCollection()
{
Records = new List<ResourceRecord>();
}
}
JSON结构
{
"Id": "1",
"Name": "myName",
"Records": [
{
"Type": "A",
"AValue": "AVal"
},
{
"Type": "B",
"BValue": "BVal"
}
]
}
的[网页API模型绑定和多态传承]可能的复制(http://stackoverflow.com/questions/17277578/web-api-model-binding-and-polymorphic-inheritence ) – Kamo
这是我在我的问题中使用的示例。为这个问题提供的答案是MVC模型绑定,我需要Web API模型绑定。 – garyamorris