WebAPI Custom Model binding of complex abstract object

匿名 (未验证) 提交于 2019-12-03 01:25:01

问题:

This is a tough one. I have an issue with binding a model from JSON. I am attempting to resolve polymorphic-ally the record supplied with the type of record that it will resolve to (I want to be able to add many record types in the future). I have attempted to use the following example to resolve my model when calling the endpoint however this example only works for MVC and not Web API applications.

I have attempted to write it using IModelBinder and BindModel(HttpActionContext actionContext, ModelBindingContext bindingContext). However I can't find the equivalent of ModelMetadataProviders in the System.Web.Http namespace.

Appreciate any help anyone can give.

I have a Web API 2 application which has the following object structure.

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 Records { get; }      public RecordCollection()     {         Records = new List();     } }

JSON Structure

{   "Id": "1",   "Name": "myName",   "Records": [     {       "Type": "A",       "AValue": "AVal"     },     {       "Type": "B",       "BValue": "BVal"     }   ] }

回答1:

After some research I discovered that metadata providers don't exist within WebAPI and in order to bind to complex abstract objects you have to write your own.

I started by writing a new model binding method, with the use of a custom type name JSon serializer and finally I updated my endpoint to use the custom binder. It's worth noting the following will only work with requests in the body, you will have to write something else for requests in the header. I would suggest a read of chapter 16 of Adam Freeman's Expert ASP.NET Web API 2 for MVC Developers and complex object binding.

I was able to serialize my object from the body of the request using the following code.

WebAPI configuration

public static class WebApiConfig {     public static void Register(HttpConfiguration config)     {         config.Services.Insert(typeof(ModelBinderProvider), 0,             new SimpleModelBinderProvider(typeof(RecordCollection), new JsonBodyModelBinder()));     } }

Custom model binder

public class JsonBodyModelBinder : IModelBinder {     public bool BindModel(HttpActionContext actionContext,         ModelBindingContext bindingContext)     {         if (bindingContext.ModelType != typeof(T))         {             return false;         }          try         {             var json = ExtractRequestJson(actionContext);              bindingContext.Model = DeserializeObjectFromJson(json);              return true;         }         catch 
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!