2
How popular the variable _clientes
guy Cliente
with the return of a consultation to a WebApi
?
Following the great suggestion of Damon Dudek I came across the error below:
public class ClienteController : Controller
{
HttpClient _client;
Uri _clienteUri;
// GET: Cliente
public ActionResult Index()
{
if (_client == null)
{
_client = new HttpClient();
_client.BaseAddress = new Uri("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 of the 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 Cliente:
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; }
}
I got it, @Damon Dudek I put the xml in the post for a better understanding and I was in doubt how to get this xml dynamically, something like that
_clientes = response // dados do xml
?– hard123
just replace the string I put in Stringreader with your
– Damon Dudek
Good morning @Damon Dudek implemented the solution again and there is type conversion error I edited the post and added an error print.
– hard123
What’s coming to your answer? Debug the object
– Damon Dudek