2014-04-08 24 views
6

請參閱從MVC此鏈接:http://aspnetwebstack.codeplex.com/discussions/351011模型的ASP.NET Web API的URI結合詞典

我有模型綁定的麻煩。從JavaScript我做一個GET Ajax請求到我的API端點叫做「/ api/products」,傳遞一些參數,包括分頁和排序作爲查詢參數。下面是完整的URI:

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

在服務器端,我有一個網頁API控制器從URI接受這些屬性:

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

「計數」和「頁」參數綁定完全正常,過濾器和排序綁定爲字典的一個實例,但其計數爲0.

我在MVC中工作,但它似乎沒有在Web API中做卡車。

+0

檢查提出的解決方案http://stackoverflow.com/questions/11950351/method-with-dictionary-parameter-in-asp-net-web-api – Veverke

回答

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) 
{ 
     //... 
} 
+0

嗨Shahrooz,我試過了,但它仍然沒有正確綁定在字典屬性 –

+0

@FanieReynders請閱讀:http://www.hanselman.com/blog/ASPNETWireFormatForModelBindingToArraysListsCollectionsDictionaries.aspx –

+0

據我所知,模型綁定在web api中的作品與mvc不同 –