這是一個棘手的問題。我有一個從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