Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/api/5.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
使用PutAsync将数据(带寄存器的模型)发送到API,但到达时没有仅注册Id_Api_Asp.net Web Api_Core - Fatal编程技术网

使用PutAsync将数据(带寄存器的模型)发送到API,但到达时没有仅注册Id

使用PutAsync将数据(带寄存器的模型)发送到API,但到达时没有仅注册Id,api,asp.net-web-api,core,Api,Asp.net Web Api,Core,我正试图通过API更新一条记录,到达时数据为空 我使用的运输方式好吗? 我应该在什么时候寄挂号信 控制器 // POST: Category/Edit/5 [HttpPost] [ValidateAntiForgeryToken] public async Task<ActionResult> Edit(string id, Category_Model cate) { HttpClient client = new HttpClient(); var categor

我正试图通过API更新一条记录,到达时数据为空

我使用的运输方式好吗? 我应该在什么时候寄挂号信

控制器

// POST: Category/Edit/5
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Edit(string id, Category_Model cate)
{
    HttpClient client = new HttpClient();
    var category = new StringContent(JsonConvert.SerializeObject(cate, Formatting.Indented));
    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
    HttpResponseMessage response = await client.PutAsync(new Uri("http://localhost:26457/api/Category/" + id), category);
    string result = await response.Content.ReadAsStringAsync();
    return RedirectToAction(nameof(Index));
}
//POST:Category/Edit/5
[HttpPost]
[ValidateAntiForgeryToken]
公共异步任务

也尝试

1-


2-

我认为您没有在API调用中传递数据的内容类型

您正在创建一个
StringContent
,但是您没有通知API内容实际上是JSON,因此它可能只是认为它是一个字符串,不会尝试将其反序列化到您的对象中

我的建议是……
不手动序列化对象并让HTTP客户端执行序列化工作:

HttpResponseMessage response = await client.PutAsync(new Uri("http://localhost:26457/api/Category/" + id), cate);
或者,将内容类型信息添加到StringContent:

var category = new StringContent(JsonConvert.SerializeObject(cate, Formatting.Indented), Encoding.UTF8, "application/json");

有关StringContent的构造函数,请参阅。

控制器签名中可能存在的重复项,请尝试在参数之前添加
[FromBody]
。e、 g.
public void Put(string id,[FromBody]Category\u Model Category)
@CraigH我正在使用它,但它没有进入API函数
var category = new StringContent(JsonConvert.SerializeObject(cate, Formatting.Indented), Encoding.UTF8, "application/json");