How popular is the variable _clientes
type Cliente
with the return of a query to a WebApi
?
Following the great suggestion of the Damon Dudek I came across the error below:
publicclassClienteController:Controller{HttpClient_client;Uri_clienteUri;//GET:ClientepublicActionResultIndex(){if(_client==null){_client=newHttpClient();_client.BaseAddress=newUri("http://localhost:58573");
_client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
}
ArrayOfCliente _clientes = Listar();
return View(_clientes);
}
private ArrayOfCliente Listar()
{
HttpResponseMessage response = _client.GetAsync("api/clientes").Result;
ArrayOfCliente oPessoa = new ArrayOfCliente();
if (response.IsSuccessStatusCode)
{
XmlSerializer serializer = new XmlSerializer(typeof(ArrayOfCliente));
//
using (TextReader reader = new StringReader(response))
{
ArrayOfCliente result = (ArrayOfCliente)serializer.Deserialize(reader);
}
}
else
{
Response.Write(response.StatusCode.ToString() + " - " + response.ReasonPhrase);
}
return oPessoa;
}
Return from WebApi:
<ArrayOfCliente xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/Dominio.Apolo.Modelo">
<Cliente>
<ClienteId>3</ClienteId>
<DtCadastro>2017-08-22T00:00:00</DtCadastro>
<Nome>Artefatos</Nome>
<RazaoSocial>Art e Fatos</RazaoSocial>
<TipoPessoa>PJ</TipoPessoa>
</Cliente>
<Cliente>
<ClienteId>4</ClienteId>
<DtCadastro>2017-08-22T00:00:00</DtCadastro>
<Nome>Empresa e Ind.</Nome>
<RazaoSocial>Nicks Oliveira</RazaoSocial>
<TipoPessoa>PJ</TipoPessoa>
</Cliente>
</ArrayOfCliente>
Model Client:
public class Cliente
{
public int ClienteId { get; set; }
public string Nome { get; set; }
public string RazaoSocial { get; set; }
public string TipoPessoa { get; set; }
public DateTime DtCadastro { get; set; }
}