Api that receives an Array of items

1

I have the following situation, I need to create an API that will receive the content below:

{
  "numeroCarrinho": 122865,
  "itens": {
    "PA00058": 1,
    "MA00068": 1,
    "PA00004": 1
  },
  "cep": "41706670",
  "retiraNoLocal": false
}

Example: localhost: 7630 / api / drive / query / cart / 122865 / PA00058: 1, MA00068: 1, PA00004: 1/41706670 / false

The problem is that the items would be dynamic, an array of items, how could I do that, I did an example more like to know how I can get all the separate items.

[HttpGet]
[Route("unidade/consulta/carrinho/{numerocarrinho}/{itens}/{cep}/{retiralocal}")]
public HttpResponseMessage ConsultaUnidadeAtendimento(string numerocarrinho, string[] itens, string cep, string retiralocal)
{

    try
    {
       var tTabela = "";
       var listar = "";
       return Request.CreateResponse(HttpStatusCode.OK, new { usuario = listar.ToArray() });
    }
    catch (Exception ex)
    {

        return Request.CreateResponse(HttpStatusCode.BadRequest, ex.Message);
    }

}
    
asked by anonymous 06.12.2017 / 20:01

2 answers

0

You can use Dynamic p>

public JObject PostSavePage(dynamic testObject)
{
    return testObject;
}

would look like this:

Update:

ExampleFullController:

usingNewtonsoft.Json.Linq;usingSystem.Web.Http;namespaceTestApp.Controllers{[RoutePrefix("api/values")]
    public class ValuesController : ApiController
    {
        [HttpPost]
        [Route("postsave")]
        public JObject PostSavePage(dynamic testObject)
        {
            return testObject;
        }
    }
}
    
06.12.2017 / 22:55
0

You could create a class that represents the data to be received by the method, so your code becomes more readable. The data would be sent to the request body.

Class:

public class ConsultaUnidadeAtendimentoModel
    {
        [JsonProperty("cep")]
        public string Cep { get; set; }

        [JsonProperty("itens")]
        public dynamic Itens { get; set; }

        [JsonProperty("numeroCarrinho")]
        public long NumeroCarrinho { get; set; }

        [JsonProperty("retiraNoLocal")]
        public bool RetiraNoLocal { get; set; }
    }

Controller:

 [Route("unidade/consultaUnidadeAtendimento")]
public HttpResponseMessage ConsultaUnidadeAtendimento(ConsultaUnidadeAtendimentoModel consultaAtendimento)
{

    try
    {
       var tTabela = "";
       var listar = "";
       return Request.CreateResponse(HttpStatusCode.OK, new { usuario = listar.ToArray() });
    }
    catch (Exception ex)
    {

        return Request.CreateResponse(HttpStatusCode.BadRequest, ex.Message);
    }
}
    
07.12.2017 / 04:00