2017-01-09 1 views
0

私は別のWeb API(remoteApi)を呼び出す.netcore web api(localApi)を持っています。 remoteApiはjsonを返します。私の目標は、そのjsonに新しいプロパティを追加し、localApiを呼び出す呼び出し元にjsonとして返します。別のAPIからAPIアクションを呼び出す、jsonレスポンスを読み込んで追加のプロパティを追加する

これまでのところ、私のコードは次のようである:

   var httpResponse = await httpClient.PostAsync(remoteApi), httpContent); 

       // If the response contains content we want to read it! 
       if (httpResponse.Content != null) 
       { 
        responseContent = await httpResponse.Content.ReadAsStringAsync(); 

        responseContent = responseContent.Insert(2, " \"isCachedResponse\": false,"); 

        var retVal = await Task.Run(() => JsonConvert.SerializeObject(responseContent)); 
        return Ok(retVal); 
       } 

このアプローチの問題は、応答が文字列ではなく、JSONであることです。 以下のように:

"\"{ \"isCachedResponse\": true, \\\"remoteApiResponse\\\":{ \\\"applicationId\\\":\\\"10001000000300071\\\", \\\"reasons\\\":[ { \\\"reason\\\":\\\"Score Cut Policy\\\" } ], \\\"decisionText\\\":\\\"Duplicate request; check UI for more information\\\", \\\"decisionCode\\\":\\\"Undecisioned\\\", \\\"officialNameOnFile\\\":{ \\\"firstName\\\":\\\"\\\", \\\"middleName\\\":\\\"\\\", \\\"lastName\\\":\\\"\\\" } } }\"" 

は、どのように私はこの問題を回避することができますか?

答えて

1

すでに代わりに、いくつかの追加の作業を開始するいくつかの手動直列化を行うと、OkObjectResultを返すのに必要なJSONを持っているので、あなたは単なる文字列ContentResultを返すことができます:

if (httpResponse.Content != null) 
{ 
    responseContent = await httpResponse.Content.ReadAsStringAsync(); 

    responseContent = responseContent.Insert(2, " \"isCachedResponse\": false,"); 

    return this.Content(responseContent, "application/json"); 
} 
+0

サンプルコードと一緒に素晴らしい説明。私はこれが他の誰かを私の場所で助けることを願っています。ありがとうございました... – w2olves

関連する問題