我写了这个工作PutAsync,它允许我更新我的数据库中的项目:
private async void Edit()
{
using (HttpClient client = new HttpClient())
{
Item i = new Item();
i.ID = SelectedItem.ID;
i.Name = SelectedItem.Name;
i.Cost = SelectedItem.Cost;
string json = JsonConvert.SerializeObject(i);
HttpResponseMessage response = await client.PutAsync("http://localhost:5065/api/item", new StringContent(json, Encoding.UTF8, "application/json"));
if (response.IsSuccessStatusCode)
{
string jsonresponse = await response.Content.ReadAsStringAsync();
int result = JsonConvert.DeserializeObject<int>(jsonresponse);
if (result == 2)
{
GetItems();
}
}
}
}
现在我想删除项目并添加新项目,但此代码似乎不起作用:
public ICommand DeleteCommand
{
get { return new RelayCommand(Delete); }
}
private async void Delete()
{
using (HttpClient client = new HttpClient())
{
Item i = new Item();
i.ID = SelectedItem.ID;
i.Name = SelectedItem.Name;
i.Cost = SelectedItem.Cost;
HttpResponseMessage response = await client.DeleteAsync("http://localhost:5065/api/item/" + i.ID );
if (response.IsSuccessStatusCode)
{
string jsonresponse = await response.Content.ReadAsStringAsync();
int result = JsonConvert.DeserializeObject<int>(jsonresponse);
if (result == 2)
{
GetItems();
}
}
}
}
我的PostAsync也是如此:
private async void New()
{
using (HttpClient client = new HttpClient())
{
Item i = new Item();
i.Name = SelectedItem.Name;
i.Cost = SelectedItem.Cost;
string json = JsonConvert.SerializeObject(i);
HttpResponseMessage response = await client.PostAsync("http://localhost:5065/api/item", new StringContent(json, Encoding.UTF8, "application/json"));
if (response.IsSuccessStatusCode)
{
string jsonresponse = await response.Content.ReadAsStringAsync();
int result = JsonConvert.DeserializeObject<int>(jsonresponse);
if (result == 2)
{
GetItems();
}
}
}
}
我收到以下回复:
{StatusCode: 204, ReasonPhrase: 'No Content', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:{ Pragma: no-cache X-SourceFiles: =?UTF-8?B?RDpcU0NIT09MXE5NQ1QgTGFhdHN0ZVxCdXNpbmVzcyBhcHBsaWNhdGlvbnNcUFJPSkVDVFxQcm9qZWN0XFByb2plY3QuYXBpXGFwaVxwcm9kdWN0XDU=?= Cache-Control: no-cache Date: Mon, 19 Jan 2015 13:48:32 GMT Server: Microsoft-IIS/8.0 X-AspNet-Version: 4.0.30319 X-Powered-By: ASP.NET Expires: -1}}
我做错了什么,如何让我的帖子和删除工作都有效?
编辑: 这是Delete void的一些服务器端代码,它非常基本:
public static int DeleteItem(int id)
{
int rowsaffected = 0;
DbTransaction trans = null;
try
{
trans = Database.BeginTransaction("ConnectionString");
string sql = "DELETE FROM Item WHERE ID=@ID";
DbParameter par1 = Database.AddParameter("ConnectionString", "@ID", id);
rowsaffected += Database.ModifyData(trans, sql, par1);
trans.Commit();
}
catch (Exception ex)
{
if (trans != null)
trans.Rollback();
}
finally
{
if (trans != null)
Database.ReleaseConnection(trans.Connection);
}
return rowsaffected;
}
我在这里打电话:
// DELETE: api/Item/5
public void Delete(int id)
{
ItemDataAccess.DeleteItem(id);
}
编辑: 也许一些额外的信息,在下面一行
int result = JsonConvert.DeserializeObject<int>(jsonresponse);
我在这两种情况下都遇到了这个错误:
A first chance exception of type 'Newtonsoft.Json.JsonSerializationException' occurred in Newtonsoft.Json.dll
Additional information: No JSON content found and type 'System.Int32' is not nullable. Path '', line 0, position 0.
答案 0 :(得分:1)
您的通话成功,204是一个成功代码,告诉客户不要期待正文中的任何数据。您没有从邮件中返回任何内容,因此服务器会在控制器方法上为void
类型自动填写204。