I have a problem in pass data list from Client to the Web API 2 by JSON. Here are my code samples
Client
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 Service - Controller class
[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);
}
when i try to pass single data object to same controller it works fine. but for LIST objects it returns error code.
StatusCode: 405, ReasonPhrase: 'Method Not Allowed'
I tried to pass exact same * list content* through third party REST client . It returned success code.
You are doing a PutAsync
into a HttpPost
action. Your api URL looks incorrect as well, should be,
http://***SrviceUrl***/api/InvoiceHeader
and action should be,
public HttpResponseMessage Put(List<InvItem> items)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.