2016-08-02 77 views
0

我這樣做後,從下面的角度代碼的WebAPI異步方法:如何返回ASP.NET WEB API從異步方法「迴應」回角碼

var app = angular.module('APItest', []); 
    app.controller('TestAPI', function ($scope, $http) { 
     debugger; 
     $scope.test = function() { 
      var test = $scope.testModel.CommandText; 
      $http({ 
       method: 'POST', 
       url: '/api/CallRestAPI', 
       data: JSON.stringify(test), 
       contentType: 'application/json', 
       dataType: 'json' 
      }).then(function successCallback(response) { 
       $scope.response = response; 
      }, function errorCallback(response) { 
       // called asynchronously if an error occurs 
       // or server returns response with an error status. 
      }); 
     }; 

}); 

這是控制器:

public class CallRestAPIController:ApiController 
{ 
    public async void PostToAPI([FromBody]string value) 
    { 
     var payload = value; 

     // Serialize our concrete class into a JSON String 
     var stringPayload = await Task.Run(() => JsonConvert.SerializeObject(payload)); 

     // Wrap our JSON inside a StringContent which then can be used by the HttpClient class 
     var httpContent = new StringContent(stringPayload, Encoding.UTF8, "application/json"); 

     using (var httpClient = new HttpClient()) 
     { 

      // Do the actual request and await the response 
      var httpResponse = await httpClient.PostAsync("https://testapi.com/prod/testapi", httpContent); 

      // If the response contains content we want to read it! 
      if (httpResponse.Content != null) 
      { 
       var responseContent = await httpResponse.Content.ReadAsStringAsync(); 
       // From here on you could deserialize the ResponseContent back again to a concrete C# type using Json.Net 
       testModel test = new testModel(); 
       object Desobj = JsonConvert.DeserializeObject(responseContent); 
       test.Response = Desobj.ToString(); 

      } 
     } 

    } 

} 

我怎樣才能返回test.Response回角successCallback功能,因爲該方法是異步,我不知道如何處理這個問題。

感謝

+0

客戶端後的數據是一個字符串:「數據:JSON .stringify(test)「,你的代碼」JsonConvert.SerializeObject(payload)「沒有效果! –

+0

爲什麼在服務器代碼中再次調用另一個API? 您可以直接在客戶端代碼中調用api! –

回答

1

試試這個:

public async testModel PostToAPI([FromBody]string value) 
    { 
     var payload = value; 
    // Serialize our concrete class into a JSON String 
    var stringPayload = await Task.Run(() => JsonConvert.SerializeObject(payload)); 

    // Wrap our JSON inside a StringContent which then can be used by the HttpClient class 
    var httpContent = new StringContent(stringPayload, Encoding.UTF8, "application/json"); 

    using (var httpClient = new HttpClient()) 
    { 

     // Do the actual request and await the response 
     var httpResponse = await httpClient.PostAsync("https://testapi.com/prod/testapi", httpContent); 

     // If the response contains content we want to read it! 
     if (httpResponse.Content != null) 
     { 
      var responseContent = await httpResponse.Content.ReadAsStringAsync(); 
      // From here on you could deserialize the ResponseContent back again to a concrete C# type using Json.Net 
      testModel test = new testModel(); 
      object Desobj = JsonConvert.DeserializeObject(responseContent); 
      test.Response = Desobj.ToString(); 
      return test;  
     } 
    } 

} 
+0

客戶端發佈的數據是一個字符串:「data:JSON.stringify(test)」,服務器代碼「JsonConvert.SerializeObject(payload)」沒有效果! –

0

通過使用Task<TResult>這可以實現如下:

public class CallRestAPIController:ApiController 
{ 
    public async Task<string> PostToAPI([FromBody]string value) 
    { 
     var payload = value; 

     // Serialize our concrete class into a JSON String 
     var stringPayload = await Task.Run(() => JsonConvert.SerializeObject(payload)); 

     // Wrap our JSON inside a StringContent which then can be used by the HttpClient class 
     var httpContent = new StringContent(stringPayload, Encoding.UTF8, "application/json"); 

     using (var httpClient = new HttpClient()) 
     { 

      // Do the actual request and await the response 
      var httpResponse = await httpClient.PostAsync("https://testapi.com/prod/testapi", httpContent); 

      // If the response contains content we want to read it! 
      if (httpResponse.Content != null) 
      { 
       var responseContent = await httpResponse.Content.ReadAsStringAsync(); 
       // From here on you could deserialize the ResponseContent back again to a concrete C# type using Json.Net 
       testModel test = new testModel(); 
       object Desobj = JsonConvert.DeserializeObject(responseContent); 
       return test.Response = Desobj.ToString(); 
      } 
      return string.Empty; 
     } 

    } 

}