从ASP.NET Web API中的URI模型绑定到Dictionary [英] Model binding to Dictionary from URI in ASP.NET Web API

查看:72
本文介绍了从ASP.NET Web API中的URI模型绑定到Dictionary的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Please refer to this link from MVC: http://aspnetwebstack.codeplex.com/discussions/351011

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

I am having trouble with model binding. From JavaScript I do an GET Ajax request to my API end point called "/api/products" passing in some parameters including paging and sorting as query parameters. Here is the complete URI:

http://localhost/api/products?page=1&count=10&filter[name]=Test1&filter[price]=10&sorting[name]=desc

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

On the server side, I have an Web API controller accepting these properties from the URI:

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

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

The "count" and "page" parameters bind perfectly fine, and the filter and sorting binds as an instance of a dictionary, but its count is 0.

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

I got this working in MVC but it doesn't seem to be doing the truck in Web API.

推荐答案

你仍然可以利用开箱即用的默认模型绑定器,你的uri看起来像

You can still leverage out of the box default model binder and your uri would look like

http:// localhost / api / products?page = 1& count = 10& filter [0] .key = name& filter [0] .value = Test1& filter [ 1] .key = price& filter [0] .value = 10& sorting [0] .key = name& sorting [0] .value = desc

这篇关于从ASP.NET Web API中的URI模型绑定到Dictionary的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆