【问题标题】:WebAPI Custom ModelBinder inheritanceWebAPI 自定义 ModelBinder 继承
【发布时间】:2013-05-07 18:13:48
【问题描述】:

我正在尝试使用 WebAPI BindParameter 绑定到某个对象类型的父级。我能够绑定到对象本身(SomeObject),但不能绑定到它的父对象(SomeOtherObject)。这将导致我必须对 SomeObject 的所有可能继承者进行特定的 BindParameter 调用。有没有更好的方法来绑定允许我完成此操作的参数类型?

活页夹:

GlobalConfiguration.Configuration.BindParameter(typeof(SomeObject), new SetTrackerModelBinder()); 

GlobalConfiguration.Configuration.BindParameter(typeof(SomeOtherObject), new SetTrackerModelBinder()); //Does not work. 

对象结构:

public class SomeObject:SomeOtherObject{

}
public class AnotherObject:SomeOtherObject{

}
public class SomeOtherObject{

}

动作:

public void PostStuffs(SomeObject value) {}

【问题讨论】:

  • 如何确定必须将哪个具体类型反序列化为基类(SomeObject 或AnotherObject)?您可以编写自定义 MediaTypeFormatter 或 ModelBinder,但您需要以某种方式确定类型。

标签: c# asp.net-mvc asp.net-web-api binders


【解决方案1】:

您可以尝试查看我对以下帖子的回答。不过我不使用 ModelBinders:

WebApi Model Binding For Inherited Types

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-28
    • 1970-01-01
    • 2011-06-25
    相关资源
    最近更新 更多