.NET Core JSON parameter

Asked

Viewed 27 times

-2

I’m starting to learn C#, using . NET CORE, and I’m trying to create an API that searches a Mongodb bank.

I’ve looked at some topics here, seen some things, but none of the solutions I found worked.

I created a class called Searchparameter to be able to receive the search parameters, but when I try to do the same in Insominia it returns the following:

inserir a descrição da imagem aqui

I can’t understand what’s going on. They follow the classes.

Searchparametrs:

public class SearchParameters
{
    // Propriedes

    [Required(ErrorMessage = "É necessário informar um parâmetro de busca.")]
    [JsonProperty("ticketField")]
    public string ticketField { get; set; } // representa o campo do ticket que será utilizado para busca.

    [Required(ErrorMessage = "É necessário informar um parâmetro de comparação.")]
    [JsonProperty("comparationParameter")]
    public string comparationParameter { get; set; } // representa o parâmetro de comparação, se o valor a ser buscado é IGUAL ou DIFERENTE do ticketField.

    [JsonProperty("initialDate")]
    public DateTime initialDate { get; set; } // representa a data incial de um período a ser pequisado.

    [JsonProperty("finalDate")]
    public DateTime finalDate { get; set; } // representa a data final de um período a ser pesquisado. 
}

CONTROLLER start, which defines the route:

[ApiController]
[Route("v1/archivedTickets")]
public class ArchivedTicketsController : ControllerBase
{...}

GET CONTROLLER METHOD:

[HttpGet]
    [Route("getByChannelName/{searchParameters}")]
    public IActionResult getByChannelName(SearchParameters searchParameters)
    {
        try
        {
            return Ok(_repository.GetByChannelName(searchParameters));
        }
        catch (Exception exc)
        {
            return null;
        }
    }

Method that performs the search in the REPOSITORY:

public List<ArchivedTicketsModel> GetByChannelName(SearchParameters searchParameters)
    {
        return _collection.Find(archivedTicket => archivedTicket.ChannelName == searchParameters.ticketField && archivedTicket.DateCreation >= searchParameters.initialDate && archivedTicket.DateCreation <= searchParameters.finalDate).ToList();
        
    }

I put a breakpoint in the CONTROLLER class to debug, but Insominia returns this message without even entering the GET method.

I’m testing on a localhost, which is my base_url. You have already received the parameters separately, and it worked, but the JSON object will not, and I want to receive this object as parameter.

1 answer

0


When you set your route this way

[Route("getByChannelName/{searchParameters}")]

Your method is waiting to receive a single parameter.

You set your method to HTTP/GET, by default it does not check what is in the body of the request to fill your parameter class. What you can do is inform you that you are waiting for an object that is in the body.

public IActionResult getByChannelName([Microsoft.AspNetCore.Mvc.FromBody]SearchParameters searchParameters)

That way, it is not necessary to pass on the route, just staying

[Route("getByChannelName")]
  • Gave straight! Thank you so much for your help!!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.