简体   繁体   中英

Input JSON on web-api POST (C#)

I currently have my web API binded to my computer files, like this:

namespace APIDissertação.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class DissertacaoController : ControllerBase
    {
        [HttpPost]
        public IActionResult Post(/*JObject InputRequest*/)
        {
            try
            {
                DissertacaoClass MeuProblema = new DissertacaoClass();
                MeuProblema.ArquivoGrafo_InicializarProblema(@"C:\C#\ArquivoGrafo.txt");
                MeuProblema.ArquivoNv0(@"C:\C#\ArquivoNv0.txt");
                MeuProblema.LerArquivoDemandas(@"C:\C#\ArquivoDemandas.txt");
                MeuProblema.GerarCaminhos_Arcos();
                String result = MeuProblema.CriarResolverProblema();
                return Ok(result);//ResultRequest);
            }
            catch (Exception ex)
            {
                return BadRequest();
            }
        }

What I want to do, is to substitute this file to a JSON that will be inputed to the API, the JSON would look like this:

{
 "QuantidadeNosSemTempo": 4
 "QuantidadeTempos": 12
 "Grafo_Terminal;X;Y;Nv0": [
  "0;0;0;1", 
  "1;2;2;1", 
  "2;3;1;1", 
  "3;0;3;1" 
 ],
 "Demanda;TerminalNoInicial;TempoNoInicial;TerminalNoFinal;TempoNoFinal;Tamanho" : [
  "0;0;0;2;8;0,7",
  "1;0;0;3;11;0,3",
  "2;1;0;2;8;0,5",
  "3;1;1;2;9;0,5",
  "4;2;1;0;10;0,3",
  "5;2;1;3;11;0,2"
 ]
}

How can I do that? I'm pretty new in web api development (actually have no idea what I'm doing),

Thanks in advance,

Tarek

You can use type dynamic if you want all structures to be accepted.

However I prefer to generate a type using a tool like https://app.quicktype.io/ and use that DTO afterwards.

Also your json structure had an issue with some missing commas

{
 "QuantidadeNosSemTempo": 4,
 "QuantidadeTempos": 12,
 "Grafo_Terminal;X;Y;Nv0": [
  "0;0;0;1", 
  "1;2;2;1", 
  "2;3;1;1", 
  "3;0;3;1" 
 ],
 "Demanda;TerminalNoInicial;TempoNoInicial;TerminalNoFinal;TempoNoFinal;Tamanho" : [
  "0;0;0;2;8;0,7",
  "1;0;0;3;11;0,3",
  "2;1;0;2;8;0,5",
  "3;1;1;2;9;0,5",
  "4;2;1;0;10;0,3",
  "5;2;1;3;11;0,2"
 ]
}

After that you simply send a http POST request with content type "application/json" and where the body of the request contains the json to that url and you should be good to go.

Just as suggestion, maybe you could change your json a litle bit, like this (note that I change the two arrays in your json by FirstArray and SecondArray ):

{
"QuantidadeNosSemTempo": 4,
"QuantidadeTempos": 12,
"FirstArray": [{
        "Grafo_Terminal": 0,
        "X": 0,
        "Y": 0,
        "Nv0": 1
    }, {
        "Grafo_Terminal": 1,
        "X": 2,
        "Y": 2,
        "Nv0": 1
    }, {
        "Grafo_Terminal": 2,
        "X": 3,
        "Y": 1,
        "Nv0": 1
    }, {
        "Grafo_Terminal": 3,
        "X": 0,
        "Y": 3,
        "Nv0": 1
    }
],
"SecondArray": [{
        "Demanda": 0,
        "TerminalNoInicial": 0,
        "TempoNoInicial": 0,
        "TerminalNoFinal": 2,
        "TempoNoFinal": 8,
        "Tamanho": 0.7
    }, {
        "Demanda": 1,
        "TerminalNoInicial": 0,
        "TempoNoInicial": 0,
        "TerminalNoFinal": 3,
        "TempoNoFinal": 11,
        "Tamanho": 0.3
    }, {
        "Demanda": 2,
        "TerminalNoInicial": 1,
        "TempoNoInicial": 0,
        "TerminalNoFinal": 2,
        "TempoNoFinal": 8,
        "Tamanho": 0.5
    }, {
        "Demanda": 3,
        "TerminalNoInicial": 1,
        "TempoNoInicial": 1,
        "TerminalNoFinal": 2,
        "TempoNoFinal": 9,
        "Tamanho": 0.5
    }, {
        "Demanda": 4,
        "TerminalNoInicial": 2,
        "TempoNoInicial": 1,
        "TerminalNoFinal": 0,
        "TempoNoFinal": 10,
        "Tamanho": 0.3
    }, {
        "Demanda": 5,
        "TerminalNoInicial": 2,
        "TempoNoInicial": 1,
        "TerminalNoFinal": 3,
        "TempoNoFinal": 11,
        "Tamanho": 0.2
    }
]

}

And your C# class that will be the input request could be a DTO ( InputRequest ):

    public class FirstArray    {
        public int Grafo_Terminal { get; set; } 
        public int X { get; set; } 
        public int Y { get; set; } 
        public int Nv0 { get; set; } 
    }

    public class SecondArray    {
        public int Demanda { get; set; } 
        public int TerminalNoInicial { get; set; } 
        public int TempoNoInicial { get; set; } 
        public int TerminalNoFinal { get; set; } 
        public int TempoNoFinal { get; set; } 
        public double Tamanho { get; set; } 
    }

    public class InputRequest    {
        public int QuantidadeNosSemTempo { get; set; } 
        public int QuantidadeTempos { get; set; } 
        public List<FirstArray> FirstArray { get; set; } 
        public List<SecondArray> SecondArray { get; set; } 
    }

So, if you implement this changes, your post method will be:

[HttpPost]
public IActionResult Post(InputRequest inputRequest)
{
    try
    {
        DissertacaoClass MeuProblema = new DissertacaoClass();
        String result = MeuProblema.CriarResolverProblema(inputRequest); //you will have to change the way you resolve the problem
        return Ok(result); //ResultRequest
    }
    catch (Exception ex)
    {
        return BadRequest();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM