2012-06-08 24 views
0

this question答案建議使用一個對象作爲參數。我將使用什麼URL訪問該URL? 在OP的第一個例子,我原來的做法是超負荷動作(不知道超載是正確的字),所以我有:帶對象參數的MVC操作的URL(使用web API)

public IEnumerable<NTOrder> Get()... 

public IEnumerable<NTOrder> Get(int p)... 

public IEnumerable<NTOrder> Get(int p, int q) 

不優雅,我知道,但如果我將其更改爲一個對象我不知道如何格式化網址...

舊代碼

public IEnumerable<NTOrder> Get() { 
    //build NTOrderList 
    return NTOrderList; 
} 

新代碼

public class FilterView 
{ 
    public int? fID { get; set; } 
    public int? fCustomer { get; set; } 
    public string fSalesPerson{ get; set; } 
} 

public IEnumerable<NTOrder> Get(FilterView queryFilter) { 
    //build NTOrderList 
    List<NTOrder> result = (from order in NTOrderList 
           where (order.OrderID == queryFilter.fID || queryFilter.fID == null) 
            && (order.CustomerID == queryFilter.fCustomer || queryFilter.fCustomer == null) 
            && (queryFilter.fSalesPerson == null || order.Salesperson.Equals(queryFilter.fSalesPerson)) 
           select order).ToList(); 

    return result; 
} 

回答

1

我發現this answer作爲一種可能的解決方案,它基本上建議將數據作爲JSON傳遞給url並添加一個過濾器將其轉換爲複雜對象,但由於該帖子中的OP表明它不是最優雅的方式。 ..任何人都有更優雅的解決方案?

0

該URL的工作方式完全相同,對象中的每個屬性都有一個querystring參數。

+0

我想...我得到一個異常的消息: 沒有MediaTypeFormatter可從與媒體類型的內容讀類型的對象「FilterView」「」不確定'。 –

+0

我已經發布了我的舊的和新的代碼片段...舊的url只是http:// localhost:3495/api/NTOrder。對於新代碼,我嘗試了各種東西,如http:// localhost:3495/api/NTOrder?fID = 101以及其他所有/無變量的組合 –

相關問題