【发布时间】:2014-01-17 12:22:06
【问题描述】:
我正在将 MongoDB 与 ASP.NET Web Api (2) 应用程序一起使用,并希望在 Web Api 方法中接受 ObjectId 参数。
我为 ObjectId 类型编写了一个自定义模型绑定器,当将它添加到控制器的 Get 方法时,一切正常。
[Route("{id}")]
public HttpResponseMessage Get(String type, [ModelBinder(typeof(ObjectIdModelBinder))]ObjectId id) {
但我需要在几个方法和控制器中执行此操作,所以我宁愿将它放在中心位置。我读过我可以像这样集中注册活页夹:
public static void Register(HttpConfiguration config) {
var provider = new SimpleModelBinderProvider(typeof(ObjectId), new ObjectIdModelBinder());
config.Services.Insert(typeof(ModelBinderProvider), 0, provider);
}
但这不起作用!
有什么想法吗?不太确定 config.Services 集合应该包含什么,但我很难找到我插入的活页夹。
【问题讨论】:
标签: asp.net-web-api model-binding