且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

放的WebAPI回报HTT presponseMessage空

更新时间:2023-02-13 10:00:58

这是没有意义的:

JsonConvert.DeserializeObjectAsync<Htt$p$psponseMessage>(response.Result.Content.ReadAsStringAsync().Result).Result

响应已经的的一个的Htt presponseMessage

 任务&LT; Htt的presponseMessage&GT;响应

有什么反序列化。所有你需要做的就是的await 的它得到它的结果。首先,让你的方法异步

 公共异步任务&LT; Htt的presponseMessage&GT; TestEdit(INT ID,测试试验)

然后等待结果的方法:

 返回等待httpClient.PutAsJsonAsync&LT;试验&gt;(URI,测试);

这将有效地返回的Htt presponseMessage 对象。所以,让这个异步以及

 公共异步任务&LT;&的ActionResult GT; TestEdit(测试试验)

和等待着你的另一种方法:

 的Htt presponseMessage objtest =等待TestDatabaseService.TestEdit(test.testID,测试);

这不是真的不清楚为什么你需要这个抽象背后多种方法,但如果语义为您的需要有意义那么这很好。有没有直接危害到它。

但基本上你想讲一个JSON解串器反序列化的东西,那么,是不是一个JSON重新presentation该对象。因此,其结果将是,因为反序列化会悄悄地失败。但问题是,你并不需要在这里反序列东西。 PutAsJsonAsync&LT; T&GT; 已返回类型的对象的Htt presponseMessage

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.

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

. 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.ReadAsStringAsy‌​nc().Result).Result; } }

This makes no sense:

JsonConvert.DeserializeObjectAsync<HttpResponseMessage>(response.Result.Content.ReadAsStringAsync().Result).Result

The response already is an HttpResponseMessage:

Task<HttpResponseMessage> response

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

This will effectively return the HttpResponseMessage object. So make this async as well:

public async Task<ActionResult> TestEdit(Test test)

And await your other method:

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.

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.