Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用参数C调用HttpPut#_C#_Asp.net - Fatal编程技术网

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