2017-02-14 62 views
0

我已经编写了Angularjs服务,如下所示。它从第三方服务中检索数据。它工作正常。Asp.net WebApi方法代替AngularJs服务

现在我有一个要求,为它写一个WebApi方法。原因是,我们可以从各种类型的application.i.e使用该服务。桌面,网页和手机。我如何实现这样的服务?

AngulaJS服务:

(function() { 
    appModule.service('getPropertyDetailsByUsingApiService', ['$http', function ($http) { 
     this.propertyDetails = function (token, number, street, county, zip) { 
      var endpointUrl = 'http://myaddress.com/api/AddressMatcher?Token='; 
      var url = endpointUrl + token + '&Number=' + number + '&Street=' + street + '&County=' + county + '&Zip=' + zip; 

      return $http.get(url).then(function (data) { 
       var result = data; 
       if (result.data[0].Status == 'OK') { 
        return $http.get(endpointUrl + token + '&Apn=' + result.data[0].Result[0].APN + '&County=' + county) 
         .then(function (finalData) { 
          return finalData; 
         }); 
       } else { 
        return null; 
       } 
      }); 
     }; 
    } 
    ]); 
})(); 

的WebAPI方法:

[HttpGet] 
    public async Task<MyModelDto> GetPropertyDetailsByUsingApiService() 
    { 
     //I would like to have a help here to implement it 

     return result; 
    } 
+0

你在输出期望?你能详细说明吗? – Pavvy

+0

我可以管理输出。我只需要知道如何在Web API中调用上述第三方服务。 @Pavvy – Sampath

+0

请检查我的答案。 – Pavvy

回答

1

我猜你正在寻找HttpClient.GetAsync

例如,

var response = await client.GetAsync("http://..."); 
if (response.IsSuccessStatusCode) { 
    ... 
} 
0

使用此,

using (HttpClient client = new HttpClient()) 
     { 
      client.BaseAddress = new Uri(apiDetails.BaseUrl); 
      client.DefaultRequestHeaders.Accept.Clear(); 
      client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));     
      HttpResponseMessage response = client.PostAsJsonAsync(apiDetails.RequestUrl, obj).Result;     
     }