Query javascript in the Asp.net controller

Asked

Viewed 44 times

-1

I need to make a query on a method of API (Javascript) of google maps, however by controller, it is possible ?

I have a requisition ajax that mounts a grid and within it I make a query in the table dbo.Atlas setting latitude and longitude to return the address, but this table does not have all the registration addresses and in this case, would need to perform this procedure in addition to return on the screen, also save in the database, so next time have this record in the table.

Codes:

public ActionResult ListaPosicoes(string pocsag)
        {

            dalOperador dalOper = new dalOperador();


            List<modOperadorDashLatLng> listaPosMapa = new List<modOperadorDashLatLng>();

            listaPosMapa = dalOper.pubEventosProcessadosDashMapa(pocsag);
            dalAtlasEndereco dalAtlas = new dalAtlasEndereco();

            foreach (var evento in listaPosMapa)
            {

                if (evento.latitude != 0 && evento.longitude != 0)
                {
                    evento.endereco = dalAtlas.pubTrasformaLatLongEndereco(evento.latitude.ToString(), evento.longitude.ToString());
                    if (string.IsNullOrEmpty(evento.endereco))// caso não exista na tabela dbo.Atlas
                    {
                        evento.endereco = ""; // Endereço que deveria retornar na API do google.Maps passando como parametros a latitude e longitude

                        //Inserir na tabela dbo.Atlas passando endereço, latitude e longitude.
                    }
                }

                else
                {
                    evento.endereco = "Não válido";
                }

            }
            var pontosMapa = Json(listaPosMapa, JsonRequestBehavior.AllowGet);

            pontosMapa.MaxJsonLength = int.MaxValue;
            return pontosMapa;
        }

1 answer

1


Good afternoon Igor, if I understand correctly, this would be an alternative for you to consume this api by the controller:

    HttpClient _servico = new HttpClient();
    ServicePointManager.ServerCertificateValidationCallback = ((sender, certificate, chain, sslPolicyErrors) => true);
    //base da url da api
    _servico.BaseAddress = new Uri("http://url");
    //Aqui vc define se deseja o retorno em json ou xml
    _servico.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
    //Se necessário autenticacao, toten...
    _servico.DefaultRequestHeaders.Add("usuario", "senha");

    HttpResponseMessage resposta = _servico.GetAsync().Result;

//resposta com sucesso    
if (resposta.IsSuccessStatusCode)
{
    //Sua variavel para guardar a lista de resultados
    listaResultados = resposta.Content.ReadAsAsync<List<ResultadoAPI>>().Result;
}

Browser other questions tagged

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