ASP.NET MVC 5 consuming apiweb

1

I am consuming an api which returns me as response the following:

{
    "rows": {
        "recordset": [
            {
                "Monto": "10000000",
                "CantidadDias": 50,
                "TasaDiaria": "0.009700000000000",
                "TasaPeriodo": "0.48",
                "MontoInteres": 48000,
                "MontoFinal": 10048000
            }
        ]
    }
}

The problem I have is that I do not know how to consume that data, starting with the model and then consuming it in the controller.

My controller: (It is a method that is in the controller)

private static async Task<object> NewMethod(object RootObject, HttpClient client)
    {
        client.BaseAddress = new Uri("http://0.0.0.0:0000/");
        client.DefaultRequestHeaders.Accept.Clear();
        client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
        client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("authentication");

        try
        {
            var department = new Rows { recordset = new List<Recordset>() };
            HttpResponseMessage response = await client.PostAsJsonAsync("api/simuladorDAP", department);

            if (response.IsSuccessStatusCode)
            {
                var EmpResponse = response.Content.ReadAsStringAsync().Result;
                RootObject = JsonConvert.DeserializeObject<Recordset>(EmpResponse);
            }
        }
        catch (Exception e)
        {

            throw (e);
        }

        return RootObject;
    }

To make the post you must pass 2 parameters, here I do not know how to fill the list that I have in this line to pass those two parameters.

The parameters are Amount and Amount.

  

var department = new Rows {recordset = new List ()};

My model:

public class Recordset
{
    public string Monto { get; set; }
    public int CantidadDias { get; set; }
    public string TasaDiaria { get; set; }
    public string TasaPeriodo { get; set; }
    public int MontoInteres { get; set; }
    public int MontoFinal { get; set; }
}

public class Rows
{
    public List<Recordset> recordset { get; set; }
}

public class RootObject
{
    public Rows rows { get; set; }
}

They could indicate to me how I should do it please or that I am wrong. Thanks.

    
asked by vcasas 04.05.2018 в 13:54
source

1 answer

0

To initialize a list in line ( inline initialization ), in this case a List<Recordset> , the first thing to keep in mind is to specify the type of the list ( Recordset in this case) ). Then, new objects of the correct type are created, and these can be initialized in line with the necessary parameters.

In summary, from your question it is deduced that you want to create an object of type Rows and initialize the parameters Monto and CantidadDias (I understand that of the first row). This would be done in the following way:

var department = new Rows { recordset = new List<Recordset>() { new Recordset() { Monto = "10", CantidadDias = 20 } } };

Although in reality I imagine that what you want is to create an object of ripo RootObject , to serialize it and send it to the api. With what would actually be something like this:

var department = new Rootobject() { rows = new Rows { recordset = new List<Recordset>() { new Recordset() { Monto = "10", CantidadDias = 20 } } } };
var jsondepartment = JsonConvert.SerializeObject(department);
HttpResponseMessage response = await client.PostAsJsonAsync("api/simuladorDAP", jsondepartment);
    
answered by 04.05.2018 в 14:33