【发布时间】:2016-11-14 17:03:32
【问题描述】:
我的 ASP.NET Web API 类有一个 GET 端点,它有各种过滤器,这些过滤器从 URI 映射到一个过滤器类:
控制器:
public IHttpActionResult List([FromUri] FilterModel filter)
过滤器类:
public class FilterModel {
public int Something {get;set;}
public int[] UserIds {get;set;}
...lots of other properties...
}
我希望将 UserIds 作为逗号分隔的列表传递:
http://foo.com/api/things?UserIds=1,2,3
但是,这是 Web API,它期望:
http://foo.com/api/things?UserIds=1&UserIds=2&UserIds=3
有没有简单的方法来解决这个问题。我看到我可以创建一个自定义模型绑定器,但是对于 99% 的模型绑定,我不想更改任何内容。我理想的解决方案是这样的:
public class CustomModelBinder : IModelBinder
{
public bool BindModel(HttpActionContext actionContext, ModelBindingContext bindingContext)
{
// Access the "default" model that's created from the Uri params
var model = ???
// Then do some work to bind the UserIds
return true;
}
}
有什么想法吗?
谢谢
【问题讨论】:
-
您需要创建一个自定义媒体类型格式化程序,例如:link
标签: c# asp.net asp.net-web-api model-binding