Doubt with data return Asp.net API

Asked

Viewed 46 times

1

I have a method that makes the record insertion, after having inserted precise return the id that was recorded, more is not returning the information. I appreciate the help.

        [HttpGet]
    [Route("consulta/InseriUsuario/{apiKey}/{userKey}/{nomecompleto}/{email}/{login}/{senha}/{celular}/{cidade}/{uf}/{idperfil}/{idpais}/{ididioma}")]
    public HttpResponseMessage InseriUsuario(string apiKey, string userKey, string NOMECOMPLETO, string EMAIL, string LOGIN, string SENHA, string CELULAR, string CIDADE, string UF, int IDPERFIL, int IDPAIS, int IDIDIOMA )
    {

        try
        {
            var tTabela = new UsuarioAplicacao();
            tTabela.InseriUsuario(apiKey, userKey, NOMECOMPLETO, EMAIL, CELULAR, CIDADE, UF, LOGIN, SENHA, IDPERFIL, IDPAIS, IDIDIOMA );
            return Request.CreateResponse(HttpStatusCode.OK, tTabela.ToString());

        }
        catch (Exception ex)
        {

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

       public string InseriUsuario(string apiKey, string userKey, string NOMECOMPLETO, string EMAIL, string CELULAR, string CIDADE, string UF,  string LOGIN, string SENHA, int IDPERFIL, int IDPAIS, int IDIDIOMA )
        {
            var retorno = 0;
            var strQuery = "";

 //insert normal
                using (contexto = new Contexto())
                {
                    var reader = contexto.ExecutaComandoComRetorno(strQuery);
                    while (reader.Read())
                    {
                        retorno = Convert.ToInt32(reader["IDUSUARIO"]);
                    }
                    reader.Close();
                }
            }
            else
            {
                strQuery = "select * from TB_USUARIO where IDUSUARIO = 0";
            }

            return retorno.ToString();
        }

2 answers

2


The Insert user method returns a string with the code that was entered. You must store this value and then return it to Response.

Try it like this:

[HttpGet]
[Route("consulta/InseriUsuario/{apiKey}/{userKey}/{nomecompleto}/{email}/{login}/{senha}/{celular}/{cidade}/{uf}/{idperfil}/{idpais}/{ididioma}")]
public HttpResponseMessage InseriUsuario(string apiKey, string userKey, string NOMECOMPLETO, string EMAIL, string LOGIN, string SENHA, string CELULAR, string CIDADE, string UF, int IDPERFIL, int IDPAIS, int IDIDIOMA )
{
    try
    {
        var tTabela = new UsuarioAplicacao();
        string codigoInserido = tTabela.InseriUsuario(apiKey, userKey, NOMECOMPLETO, EMAIL, CELULAR, CIDADE, UF, LOGIN, SENHA, IDPERFIL, IDPAIS, IDIDIOMA );
        return Request.CreateResponse(HttpStatusCode.OK, codigoInserido);
    }
    catch (Exception ex)
    {
        return Request.CreateResponse(HttpStatusCode.BadRequest, ex.Message);
    }
}
  • 2

    Rovann, very grateful for the help, worked out, made the adjustment Return Request.Createresponse(Httpstatuscode.OK, new { usuario = codeInserted } );

0

In your code 'Inseriusuario', it seems to me that is missing the 'context.Savechanges()' or 'context.Savechangesasync()'.

Another thing, is returning this method as 'string' - better change to return an int which is usually the ID that has just been created and then would use it in 'Request.Createresponse'.

  • 1

    Ronaldo, I appreciate the help, no more and that, even if I add an int or string, in the same, I do not have a return api,

Browser other questions tagged

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