HttpGet parameter is not detected

0

Good afternoon guys, I do not know why my parameter is not recognized, can you help me? The value does not arrive in C # but is present in the angle C #

[HttpGet]
[Route("carregarUsuarioPorId")]
public HttpResponseMessage BuscarUsuarioPorId()
{
        try
        {
            WSS_RetornoListaDTO<Usuario> wss = new WSS_RetornoListaDTO<Usuario>();

            int idUser = 0;

            var req = Request.GetQueryNameValuePairs();

            if (req != null)
            {
                foreach (var parameter in req)
                {
                    if (parameter.Key.Equals("id"))
                        idUser = int.Parse(parameter.Value.ToString());
                }
            }

            usuarioDAO = new UsuarioDAO();
            wss.resultado = usuarioDAO.BuscarUsuarioPorId(idUser);
            if (wss.resultado.Count > 0)
            {
                wss.codRetorno = 200;
                wss.msgRetorno = "Sucesso";

                return Request.CreateResponse<WSS_RetornoListaDTO<Usuario>>(HttpStatusCode.OK, wss);
            }
            else
                return Request.CreateResponse(HttpStatusCode.NoContent, "Usuario não localizado");
        }
        catch (Exception e)
        {
            return Request.CreateResponse(HttpStatusCode.BadRequest, "ERRO - " + e.Message);
        }
}

Parameter call:

getUserPorId: function (idUser) {
    var id = [];
    id[0] = idUser;
    return 
    $http.get('http://localhost:61223/usuario/carregarUsuarioPorId',id)
      .then(function (response) {
        return response.data;
      });
  },
    
asked by anonymous 09.09.2017 / 21:48

2 answers

3

On call

public HttpResponseMessage BuscarUsuarioPorId()

The parameter does not exist. The api will not recognize, because your RouteConfig is probably saying id is optional. Put it this way

public HttpResponseMessage BuscarUsuarioPorId(int id)

And you'll save all this boilerplate code

var req = Request.GetQueryNameValuePairs();

if (req != null)
{
    foreach (var parameter in req)
    {
        if (parameter.Key.Equals("id"))
            idUser = int.Parse(parameter.Value.ToString());
    }
}
    
11.09.2017 / 13:37
1

To pass parameters in query of $http#get you must pass% object config as second parameter:

var params = {};

params.id = id;

$http.get('http://localhost:61223/usuario/carregarUsuarioPorId', {params: params}).then(function(response) {
  return response.data;
});
    
11.09.2017 / 15:36