我能夠在客戶端調用Web API方法,現在我想讓它在C#代碼中。在這裏,我正在寫我的jQuery代碼。調用Web API獲取使用數據參數的方法c#
$(document).ready(function()
{
$('#btnSubmit').click(function()
{
var Params =
{
AsOndate: Todate,
BCRefCode: 100,
AccID: 90000
};
$.ajax({
type: "GET",
url: 'http://localhost:51093/api/account/',
//url: 'http://192.168.0.171:51093/api/account/',
data: Params,
dataType: "json",
traditional: true,
success: ajaxSuccess,
error: ajaxError
});
});
,我調用Web API方法
public IEnumerable GetAccountListForMapping(Params param)
{
AccList _AccList = new AccList();
ListParams lstParam = new ListParams();
//lstParam.Add("@FromDate", Fromdate);
lstParam.Add("@AsOnDate", param.AsOndate);
lstParam.Add("@BCRefCode", param.BCRefCode);
lstParam.Add("@AccID", param.AccID);
_AccList = (AccrList)_AccList.GetAccountMappedList(lstParam);
return _AccList;
}
這是工作中的jQuery呼叫好..以及如何編寫相同的C#代碼
這就是我想
Params param1 = new Params();
param1.AsOndate = System.DateTime.Today;
param1.AccID = 90000;
param1.BCRefCode = 100;
HttpClient client = new HttpClient();
client.BaseAddress = new Uri("http://localhost:51093/");
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage response = client.GetAsync("/api/account", param1, new JsonMediaTypeFormatter()).Result;
if (response.IsSuccessStatusCode)
{.....
}
我得到的錯誤:錯誤「System.Net.Http.HttpClient.GetAsync(串的最佳重載的方法匹配, System.Net.Http.HttpCompletionOption)'有一些無效的參數 –
現在好了。它發射正確的方法並返回一些「響應」。我們如何反序列化這個迴應? –