我正在使用JQuery和Jqgrid的MVC 4 Web API,直到現在我將多個數據發佈到我的後控制器操作。在將多個參數傳遞給MVC中的動作時,感到困惑是否使用GET或POST 4
我的行動看起來像下面的一個...
[ActionName("FetchProducts")]
public List<ABC> PostProducts(Product model)
{
return _service.GetSomething(model);
}
public class Product{
public string Name {get;set;}
public string Category {get;set;}
//.... and alteast 5 more properties
}
和我的jQuery的電話是類似下面的一個...
$.ajax({
type: "POST",
url: /api/FetchProduct,
data: this.getData(),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: callback
});
function getData(){
return JSON.stringify({
Name: "from somewhere" ,
Category: "from somewhere",
Price: "from somewhere",
ABC: "from somewhere",
XYZ: "from somewhere",
//... and many more....
});
}
和這個作品!但我的朋友在工作說
我實際上只是獲取數據,應該使用'GET'而不是'POST'。由於GET用於檢索遠程數據,並且POST用於插入/更新遠程數據。
我也覺得他是對的。那麼我應該如何使用'GET'來做到這一點?
我是否必須將所有這些參數(至少有10個)作爲查詢字符串?
像如:api/FetchProduct/?Name='aaa'&&Category='vvv'&&.........
所以我的問題是什麼人應該在這樣的情況呢?我想知道其他開發者對此有何想法。謝謝
默認ModelBinder不能使用Get Params。通常,個人更喜歡使用POST,因爲它可能並不是完全需要的,但是這肯定會在過濾器數據增加時產生問題。所以它更好地根據我使用POST。 – bhuvin