Consuming web api with various models

Asked

Viewed 428 times

1

In that question of mine Doubt Verb GET Webapi I face some mistakes because of relationships. But now taking it as a basis on account of my models, how could I consume this service? Because the Tabela1 model has relationships and I needed to bring them to the MVC that I’m using to consume... But what happens is that relationships, models, do not come filled, comes null...

My controller on Asp.net mvc:

    public ActionResult Index()
    {
        //Varialvel resposável por receber os dados que estão no banco de dados para mostrar na tela
        HttpResponseMessage resposta = cliente.GetAsync("http://meuservico/api/servico").Result;

        //Se der tudo certo, é porque trouxe os dados retornando código 200
        if (resposta.IsSuccessStatusCode)
        {
            //Formata o json para mostrar na tela
            var result = resposta.Content.ReadAsAsync<IEnumerable<Tabela1>>().Result;

            //retorna para a tela os json formatado
            return View(result);
        }

        //Caso tenha dado errado, retorna somente a tela sem dado algum
        return View();
    }

The result variable brings the model Table 1 filled in, but their relationships do not... What could be happening?

1 answer

0


According to the link response code:

public IQueryable GetVerb()
{
    var dados = from d in db.Tabela1
                join a in db.Tabela2 on d.Tabela1_Tabela2Id equals a.Tabela2Id
                select new 
                {
                    nome = d.Nome,
                    cnpj = d.CNPJ,
                    endereco = a.Endereco,
                    cidade = a.Cidade,
                    cep = a.CEP,
                };

    return (dados);             
}

The method GetVerb() returns a list of a generic that was created in select. So if you are restricting to return all records from Tabela1 along with your relationships Tabela2, there is no way consumers of your API receive this data.

public IQueryable GetVerb()
{
    var dados = from d in db.Tabela1
                join a in db.Tabela2 on d.Tabela1_Tabela2Id equals a.Tabela2Id
                select new 
                {
                    nome = d.Nome,
                    cnpj = d.CNPJ,
                    endereco = a.Endereco,
                    cidade = a.Cidade,
                    cep = a.CEP,
                    tabela2 = a // <====
                };

    return (dados);             
}

Finally, make all your flow asynchronous:

public async Task<ActionResult> Index()
{
    var response = await cliente.GetAsync("http://meuservico/api/servico");
    if (!response.IsSuccessStatusCode) return View();

    var list = await response.Content.ReadAsAsync<IEnumerable<Table1>>();
    return View(list);
}
  • And if you use a Include? I say Table1.Include(x => x.Table2); Works gives same shape?

  • But then you’ll be using Lambda Expressions with Fluent API and not just Linq to SQL. Which I think is much better, and much simpler. Just do something like db.Tabela1.Include(t => t.Tabela2). But don’t forget to define relationships before.

  • So, in the web api I did so and it worked... But it’s in ASP.net MVC... How can I consume this way? You already have all the relationships...

  • Linq, Lambda Expressions, Fluent api, none has dependency with System.web. Vc can use them even with console app.

  • You could increase the response in the way you showed me then?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.