[英]Pass LIST object by Rest API Client to Web API 2 using JSON
我在通過JSON將數據從客戶端傳遞到Web API 2時遇到問題。 這是我的代碼示例
客戶
string RestUrl = "http://***SrviceUrl***/api/InvoiceHeader/{0}";
var uri = new Uri(string.Format(RestUrl, string.Empty));
List<InvItem> myList = new List<InvItem>(Common.invitems);
var json = JsonConvert.SerializeObject(myList);
var content = new StringContent(json, Encoding.UTF8, "application/json");
HttpResponseMessage response = null;
response = await client.PutAsync(uri ,content);
if (response.IsSuccessStatusCode)
{
Debug.WriteLine(@"successfully saved.");
}
Web服務-控制器類
[HttpPut]
[BasicAuthentication(RequireSsl = false)]
public HttpResponseMessage Put(string item)
{
List<InvItemToSave> myList = new List<InvItemToSave>();
myList = JsonConvert.DeserializeObject<List<InvItemToSave>>(item);
try
{
todoService.InsertInvoiceDetail(myList);
}
catch (Exception)
{
return base.BuildErrorResult(HttpStatusCode.BadRequest, ErrorCode.CouldNotCreateItem.ToString());
}
return base.BuildSuccessResult(HttpStatusCode.Created);
}
當我嘗試將單個數據對象傳遞到同一控制器時,它可以正常工作。 但是對於LIST對象,它返回錯誤代碼。
StatusCode:405,ReasonPhrase:“不允許使用方法”
我試圖通過第三方REST客戶端傳遞完全相同的*列表內容*。 它返回成功代碼。
您正在將PutAsync
放入HttpPost
操作中。 您的api URL看起來也不正確,應該是,
http://***SrviceUrl***/api/InvoiceHeader
行動應該是
public HttpResponseMessage Put(List<InvItem> items)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.