使用参数C#调用HttpPut

时间:2016-02-24 17:16:33

标签: c# asp.net

所以我在ASP.NET web api中创建了[Route("api/Account/Save")] [HttpPut] public IHttpActionResult SaveAccount(Account acc) { // do stuff } 方法。

Account

我在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);

现在我想从控制台应用程序中调用它。我试图这样做,但它不起作用。它也没有抛出任何例外。

"{\"AccountID\":1234,\"AccountName\":\"zzzzP\"}"
杰森回来了:

//table[@id="data_configuration_variables_ct_fields_body"]
    //tr[td[2]//span = "Name" and td[5]//span = "CRM"]
        /td[1]/div/input

1 个答案:

答案 0 :(得分:0)

你可能想要这样的东西

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();
        }
    }
}