javascript query in asp.net controller [closed]

-1

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

I have a ajax request that assembles a grid and inside it I make a query in the dbo.Atlas table by setting latitude and longitude to return the address, but this table does not have all the address registers and in this case, procedure in addition to returning on the screen, also save in the database, so next time have that 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;
        }
    
asked by anonymous 26.09.2018 / 20:32

1 answer

1

Good afternoon Igor, if I understood 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;
}
    
28.09.2018 / 21:53