WebAPI Put 返回 HTTPResponseMessage null [英] WebAPI Put returns HTTPResponseMessage null
问题描述
我需要实现简单的编辑功能.我正在使用 webapi 服务来更新我的测试对象.我正在从控制器发布请求调用以下方法.
I have a requirement to implement simple edit functionality.I am using webapi service to update my test object. I am calling the below method from the controller post request.
这是在测试调用中调用方法的控制器,它反过来调用 put 服务
This is the controller which calls a metod in test calls wich inturn calls the put service
public ActionResult TestEdit(Test test)
{
if (ModelState.IsValid)
{
// objTest is returned null
HttpResponseMessage objtest = TestDatabaseService.TestEdit(test.testID, test);
}
}
// Method which calls put service testDataService
public HttpResponseMessage TestEdit(int id, Test test)**
{
string uri = baseUri + "Test/" + id;
using (HttpClient httpClient = new HttpClient())
{
Task<HttpResponseMessage> response = httpClient.PutAsJsonAsync<Test>(uri, application);
return response.Result;
}
}
// The webapi service put method
public HttpResponseMessage PutTest(int id, Test test)
{
if (ModelState.IsValid && id == test).testID)
{
db.Entry(test)).State = EntityState.Modified;
try
{
db.SaveChanges();
}
catch (DbUpdateConcurrencyException)
{
return Request.CreateResponse(HttpStatusCode.NotFound);
}
// The status code is set to indicate the save is success
return Request.CreateResponse(HttpStatusCode.OK);
}
else
{
// If save failed
return Request.CreateResponse(HttpStatusCode.BadRequest);
}
}
.公共应用程序测试创建(测试测试){ string uri = baseUri + "Test";使用 (HttpClient httpClient = new HttpClient()){ 任务响应 = httpClient.PostAsJsonAsync(uri, test);return JsonConvert.DeserializeObjectAsync(response.Result.Content.ReadAsStringAsy nc().Result).Result;}}
. public Application TestCreate(Test test) { string uri = baseUri + "Test"; using (HttpClient httpClient = new HttpClient()) { Task response = httpClient.PostAsJsonAsync(uri, test); return JsonConvert.DeserializeObjectAsync(response.Result.Content.ReadAsStringAsync().Result).Result; } }
推荐答案
这毫无意义:
JsonConvert.DeserializeObjectAsync<HttpResponseMessage>(response.Result.Content.ReadAsStringAsync().Result).Result
响应已经是一个HttpResponseMessage
:
Task<HttpResponseMessage> response
没有什么可以反序列化的.你所要做的就是等待它得到它的结果.首先,让你的方法async
:
There's nothing to deserialize. All you have to do is await it to get its result. First, make your method async
:
public async Task<HttpResponseMessage> TestEdit(int id, Test test)
然后在方法中等待结果:
Then await the result in the method:
return await httpClient.PutAsJsonAsync<Test>(uri, test);
这将有效地返回 HttpResponseMessage
对象.所以也要使这个async
:
This will effectively return the HttpResponseMessage
object. So make this async
as well:
public async Task<ActionResult> TestEdit(Test test)
并等待您的其他方法:
HttpResponseMessage objtest = await TestDatabaseService.TestEdit(test.testID, test);
不太清楚为什么需要在多个方法后面抽象它,但是如果语义对您的需求有意义,那就没问题了.对它没有直接的伤害.
It's not really clear why you need to abstract this behind multiple methods, but if the semantics make sense for your needs then that's fine. There's no immediate harm to it.
但基本上,您是在尝试告诉 JSON 反序列化器反序列化某些不是 JSON 表示的对象.所以结果将是null
,因为反序列化会悄悄地失败.但关键是你不需要在这里反序列化任何东西.PutAsJsonAsync
已返回 HttpResponseMessage
类型的对象.
But basically you're trying to tell a JSON de-serializer to de-serialize something that, well, isn't a JSON representation that object. So the result will be null
, because the de-serialization will quietly fail. But the point is that you don't need to de-serialize anything here. PutAsJsonAsync<T>
already returns an object of type HttpResponseMessage
.
这篇关于WebAPI Put 返回 HTTPResponseMessage null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!