【发布时间】: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