Doubt with data feedback Asp.net API

1

I have a method that does the record insertion, after having inserted accurate return the id that was written, but is not returning the information. Thanks for 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();
        }
    
asked by anonymous 24.10.2017 / 13:49

2 answers

1

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

Try 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);
    }
}
    
24.10.2017 / 13:55
0

In your 'InseriUsuario' code, it seems that the 'context.SaveChanges ()' or 'context.SaveChangesAsync ()' is missing.

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

    
24.10.2017 / 14:55