0

I am working on consuming web api's from asp.net web applications and now I have this trouble. I am using my web api just to retrieve data from my db and for it's security with jwt. On the other hand, I have this mvc web application which consumes this web api. When I run my web-api, it's all okay, I can easily retrieve the data that I want, but it's not happening the same from my web app. Can someone please help me get familiar with serializing and why it's not reading the data from my db ? The error is popping up when the breakpoint reaches at the " readTask.Wait(); " line.

This is my WebApi controller:

public IHttpActionResult GetById (string id)
{
    ConnectionString();
    con.Open();
    SqlCommand com = new SqlCommand("oid_validation", con);
    com.CommandType = CommandType.StoredProcedure;
    com.Parameters.Add(new SqlParameter("@oid", id));
    SqlDataReader dr = com.ExecuteReader();
    DataTable dt = new DataTable();
    dt.Load(dr);
    dt.TableName = "mytable";
    return Ok(dt);
}

This is my model class and it is the same one on both my api and my web app.

public class v_websearch 
{
public system.guid ID {get; set;}
public system.DateTime DateSale {get; set;}
public system.DateTime From {get; set;}
public system.DateTime To {get; set;}
}

This is my web app controller used for consuming api's data:

    public async Task<ActionResult> Confirm(string id)
    {
                var token = (string)Session["access_token"];
                V_WebSearch dt = null;
                using (var client2 = new HttpClient())
                {
                    client2.BaseAddress = new Uri("http://localhost:63443/api/");
                    client2.DefaultRequestHeaders.Add("Authorization", $"Bearer {token}");
                    var responseTask = client2.GetAsync("default/" + id);

                    var result = responseTask.Result;

                    #region Error Handlers
                    if (result.IsSuccessStatusCode)
                    {
                        var readTask = result.Content.ReadAsAsync<V_WebSearch>();
                        readTask.Wait();
                        dt = readTask.Result;
                        return View(dt);
                    }
               }
      }

1 Answers1

0

I like SOLID principles! xD.

  • You can see Http response of GetById (review the json response) it doesn't seem to be how you expect.

  • ForHttpClient try GetFromJsonAsync Method.

  • Please add further details to expand on your answer, such as working code or documentation citations. – Community Aug 31 '21 at 13:35
  • Hi @Hector , thank you for your response. 1). I have added HttpResponse since it is an API and I wanted to see if it can retrieve any data or not, do you think I should change it to something else? 2). I need to put HttpClient because the web application is the client in this case which is consuming the API. At least this was my idea of this work, if you think we can make it work in another way, tell me pls. –  Aug 31 '21 at 14:13
  • Hi!, I think that you Api response (Http response from GetById ) is not formatted expected. Can you shared this Json response? – Héctor Damián Simañuk Sep 01 '21 at 18:35
  • Hi, my response from this code was working only for my api, it showed me the data only when I was running the API, but not at my mvc web app. But somehow I managed to do that and now it's working properly. Thank you for your time, much appreciated. –  Sep 02 '21 at 07:00