C# 使用参数C调用HttpPut#
因此,我在C# 使用参数C调用HttpPut#,c#,asp.net,C#,Asp.net,因此,我在ASP.NETWebAPI中创建了一个HttpPut方法 [Route("api/Account/Save")] [HttpPut] public IHttpActionResult SaveAccount(Account acc) { // do stuff } 我在帐户类的瞬间传递 class Account { public int AccountID { get; set; } public string AccountName { get; set;
ASP.NETWebAPI
中创建了一个HttpPut
方法
[Route("api/Account/Save")]
[HttpPut]
public IHttpActionResult SaveAccount(Account acc) {
// do stuff
}
我在帐户
类的瞬间传递
class Account
{
public int AccountID { get; set; }
public string AccountName { get; set; }
}
现在我想从控制台应用程序调用它。我正试图这样做,但它不起作用。它也不会抛出任何异常
var acc = new Account() { AccountID = 1234, AccountName = "zzzzP" };
string json = JsonConvert.SerializeObject(acc);
HttpContent content = new StringContent(json);
response = await client.PutAsync("api/Account/Save", content);
返回的Json:
"{\"AccountID\":1234,\"AccountName\":\"zzzzP\"}"
你可能想要这样的东西
static async Task PutAccount()
{
using (HttpClient client = new HttpClient())
{
client.BaseAddress = new Uri("http://yourWebSite.com");
var acc = new Account() { AccountID = 1234, AccountName = "zzzzP" };
string json = JsonConvert.SerializeObject(acc);
using (HttpResponseMessage response = await client.PutAsync("api/Account/Save", new StringContent(json)))
{
return response.EnsureSuccessStatusCode();
}
}
}
序列化json后抛出一个断点,看看json的值是什么?我刚刚在文章中更新了json