Sampath
Sampath

Reputation: 65870

Asp.net WebApi method instead of AngularJs Service

I have written Angularjs service as shown below.It retrieves data from the 3rd party service.It's working fine.

Now I have a requirement to write a WebApi method for the same.The reason for that is, we can consume that service from various types of applications.i.e. desktop, web and mobile.How can I implement such a service?

AngulaJS service:

(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 method :

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

        return result;
    }

Upvotes: 0

Views: 111

Answers (2)

Pavvy
Pavvy

Reputation: 348

Use this,

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

Upvotes: 0

Dante May Code
Dante May Code

Reputation: 11247

I guess you are looking for HttpClient.GetAsync.

For example,

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

Upvotes: 1

Related Questions