xvd*_*iff 5 c# asp.net-mvc model-binding asp.net-web-api asp.net-web-api2
我有一个通用接口,表示实体的查询选项:
public interface IQueryOptions<TEntity> {
IQueryable<TEntity> Apply(IQueryable<TEntity> query);
}
Run Code Online (Sandbox Code Playgroud)
现在我想使用查询字符串将查询选项传递到 WebApi 路由,并使用自定义模型绑定器构造 IQueryOptions。
(IQueryOptions 可以是 SortingOptions<> 或 PagingOptions<>,具体取决于传递给 api 的查询字符串)
public class DummyController : ApiController {
// optional parameter - query options are not required
public void Test([ModelBinder]IQueryOptions<MyEntity> queryOptions = null)
{
...
}
}
Run Code Online (Sandbox Code Playgroud)
public class QueryOptionsModelBinder : IModelBinder {
public bool BindModel(HttpActionContext actionContext...
}
Run Code Online (Sandbox Code Playgroud)
var queryProvider = new SimpleModelBinderProvider(typeof(IQueryOptions<>), new QueryOptionsModelBinder());
config.Services.Insert(typeof(ModelBinderProvider), 0, queryProvider);
Run Code Online (Sandbox Code Playgroud)
我目前无法让它工作。API 抛出以下错误:
Cannot create an instance of an interface.
Run Code Online (Sandbox Code Playgroud)
...这表明我的自定义模型绑定器永远不会被调用。模型绑定不适用于泛型类型吗?我尝试编写自己的模型绑定程序提供程序,但这也不起作用。
| 归档时间: |
|
| 查看次数: |
1057 次 |
| 最近记录: |