从ASP.NET Web API中的URI模型绑定到Dictionary

Fan*_*ers 6 javascript c# asp.net asp.net-web-api

请参阅MVC的这个链接:http://aspnetwebstack.codeplex.com/discussions/351011

我遇到模型绑定问题.从JavaScript我执行GET Ajax请求到我的API端点"/ api/products",传递一些参数,包括分页和排序作为查询参数.这是完整的URI:

http://localhost/api/products?page=1&count=10&filter[name]=Test1&filter[price]=10&sorting[name]=desc
Run Code Online (Sandbox Code Playgroud)

在服务器端,我有一个Web API控制器从URI接受这些属性:

public async IHttpActionResult Get([FromUri]Dictionary<string,string> filter, [FromUri]Dictionary<string,string> sorting, int count, int page)
{
        //...
}
Run Code Online (Sandbox Code Playgroud)

"count"和"page"参数绑定完全正常,过滤器和排序绑定为字典的实例,但其计数为0.

我在MVC中使用了它,但它似乎没有在Web API中使用卡车.

小智 0

我认为最好使用模型作为参数,例如:

Public class model
{
Public Dictionary<string,string> filter{get;set;}
Public Dictionary<string,string> sorting{get;set;}
Public int sorting{get;set;}
}

public async IHttpActionResult Get(model yourModel)
{
        //...
}
Run Code Online (Sandbox Code Playgroud)