Organizational Research By

Surprising Reserch Topic

how to consume a webapi from asp net web api to store result in database


how to consume a webapi from asp net web api to store result in database  using -'c#,asp.net,asp.net-web-api'

How to consume a WEBAPI from another ASP.Net Web API to store the response in a database.
I know how to consume a WEBAPI from clients like javascript,console application etc.

But the requirement is to pull the data from third party API by my WEBAPI & store the result in a database so that using my WEBAPI my clients request me for data.

How can it be possible using Asp.Net Web API ?
    

asked Sep 14, 2015 by ManieEddingt
0 votes
19 views



Related Hot Questions

3 Answers

0 votes

In this tutorial is explained how to consume a web api with C#, in this example a console application is used, but you can also use another web api to consume of course.

http://www.asp.net/web-api/overview/web-api-clients/calling-a-web-api-from-a-net-client

You should have a look at the HttpClient

HttpClient client = new HttpClient();
client.BaseAddress = new Uri("http://localhost/yourwebapi");

Make sure your requests ask for the response in JSON using the Accept header like this:

client.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue("application/json"));

Now comes the part that differs from the tutorial, make sure you have the same objects as the other WEB API, if not, then you have to map the objects to your own objects. ASP.NET will convert the JSON you receive to the object you want it to be.

HttpResponseMessage response = client.GetAsync("api/yourcustomobjects").Result;
if (response.IsSuccessStatusCode)
{
    var yourcustomobjects = response.Content.ReadAsAsync>().Result;
    foreach (var x in yourcustomobjects)
    {
        //Call your store method and pass in your own object
        SaveCustomObjectToDB(x);
    }
}
else
{
    //Something has gone wrong, handle it here
}
answered Sep 14, 2015 by tknfvdrfj
0 votes

For some unexplained reason this solution doesn't work for me (maybe some incompatibility of types), so I came up with a solution for myself:

HttpResponseMessage response = await client.GetAsync("api/yourcustomobjects");
if (response.IsSuccessStatusCode)
{
    var data = await response.Content.ReadAsStringAsync();
    var product = JsonConvert.DeserializeObject(data);
}

This way my content is parsed into a JSON string and then I convert it to my object.

answered Sep 14, 2015 by LemZij
0 votes
 public class EmployeeApiController : ApiController
    {
        private readonly IEmployee _employeeRepositary;

        public EmployeeApiController()
        {
            _employeeRepositary = new EmployeeRepositary();
        }

        public async Task Create(EmployeeModel Employee)
        {
            var returnStatus = await _employeeRepositary.Create(Employee);
            return Request.CreateResponse(HttpStatusCode.OK, returnStatus);
        }
    }   



Persistance
    public async Task Create(EmployeeModel Employee)
        {    
        var responseStatusViewModel = new ResponseStatusViewModel();
        var connection = new SqlConnection(EmployeeConfig.EmployeeConnectionString);
                    var command = new SqlCommand("usp_CreateEmployee", connection);
                    command.CommandType = CommandType.StoredProcedure;
                    var pEmployeeName = new SqlParameter("@EmployeeName", SqlDbType.VarChar, 50);
                    pEmployeeName.Value = Employee.EmployeeName;
                    command.Parameters.Add(pEmployeeName);


                    try
                    {
                        await connection.OpenAsync();
                        await command.ExecuteNonQueryAsync();

                        command.Dispose();
                        connection.Dispose();

                    }
                    catch (Exception ex)
                    {

                        throw ex;
                    }
                    return responseStatusViewModel;
                }



Repository

Task Create(EmployeeModel Employee);


public class EmployeeConfig
    {
        public static string EmployeeConnectionString;
        private const string EmployeeConnectionStringKey = "EmployeeConnectionString";
        public static void InitializeConfig()
        {
            EmployeeConnectionString = GetConnectionStringValue(EmployeeConnectionStringKey);
        }

        private static string GetConnectionStringValue(string connectionStringName)
        {
            return Convert.ToString(ConfigurationManager.ConnectionStrings[connectionStringName]);
        }
    }
answered Sep 14, 2015 by MonCasanova

...