How to return query for a string using WebAPI?

1

I have the following code which gives me an error:

  

The inline constraint of type 'DefaultInlineConstraintResolver' was unable to resolve the following inline constraint: 'String'.

     

Exception Details: System.InvalidOperationException: The inline constraint resolving of type 'DefaultInlineConstraintResolver' was unable to resolve the following inline constraint: 'String'.

        //http://localhost:1608/api/ApiCidade/consulta/clienteLoginSenha/
        [HttpGet]
        [Route("consulta/clienteLoginSenha/{email:long};{senha:long}")]
        public HttpResponseMessage ClientePorLoginSenha(string email, string senha)
        {
            try
            {
                var tCliente = new ClienteAplicacao();
                var listarDeClientes = tCliente.ListarPorLoginSenha(email,senha);
                return Request.CreateResponse(HttpStatusCode.OK, listarDeClientes.ToArray());
            }
            catch (Exception ex)
            {

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





//http://localhost:1608/api/ApiCidade/consulta/clientePorNome/e
[HttpGet]
[Route("consulta/clientePorNome/{nome:String}")]
public HttpResponseMessage ClientePorNome(string nome)
{
    try
    {
        var tCliente = new ClienteAplicacao();
        var listarDeClientes = tCliente.ListarPorNome(nome);
        return Request.CreateResponse(HttpStatusCode.OK, listarDeClientes.ToArray());
    }
    catch (Exception ex)
    {
        return Request.CreateResponse(HttpStatusCode.BadRequest, ex.Message);
    }
}

query that is in the application:

        public List<Cliente> ListarPorNome(string nome)
        {
            var strQuery = string.Format("select * from clientes where nome like {0}", nome + '%');

            using (contexto = new Contexto())
            {
                var retornoDataReader = contexto.ExecutaComandoComRetorno(strQuery);
                return TransformaReaderEmListaObjetos(retornoDataReader);
            }

        }


     private List<Cliente> TransformaReaderEmListaObjetos(SqlDataReader reader)
        {
            var clientes = new List<Cliente>();
            while (reader.Read())
            {

                Cliente cliente = new Cliente()
                {
                    Id = reader["id"] == DBNull.Value ? 0 : Convert.ToInt32(reader["id"]),
                    Nome = reader["nome"] == DBNull.Value ? string.Empty : reader["nome"].ToString(),
                    DataNascimento = reader["data_nascimento"] == DBNull.Value ? DateTime.MinValue : Convert.ToDateTime(reader["data_nascimento"]),
                    Email = reader["email"] == DBNull.Value ? string.Empty : reader["email"].ToString()

                };

                clientes.Add(cliente);
            }
            reader.Close();
            return clientes;
        }
    
asked by anonymous 06.11.2015 / 03:20

1 answer

0

:String is not a valid constraint for route configuration. Below is a link of allowed constraints for route configuration. Make sure that without this constraint your code continues to raise this error.

{ "bool", typeof(BoolRouteConstraint) },
{ "datetime", typeof(DateTimeRouteConstraint) },
{ "decimal", typeof(DecimalRouteConstraint) },
{ "double", typeof(DoubleRouteConstraint) },
{ "float", typeof(FloatRouteConstraint) },
{ "guid", typeof(GuidRouteConstraint) },
{ "int", typeof(IntRouteConstraint) },
{ "long", typeof(LongRouteConstraint) },

// Length constraints
{ "minlength", typeof(MinLengthRouteConstraint) },
{ "maxlength", typeof(MaxLengthRouteConstraint) },
{ "length", typeof(LengthRouteConstraint) },

// Min/Max value constraints
{ "min", typeof(MinRouteConstraint) },
{ "max", typeof(MaxRouteConstraint) },
{ "range", typeof(RangeRouteConstraint) },

// Regex-based constraints
{ "alpha", typeof(AlphaRouteConstraint) },
{ "regex", typeof(RegexRouteConstraint) }

You can use AlphaRouteConstraint to restrict the parameter to only contain letters of the Latin alphabet A-Z , including uppercase and lowercase letters.

Example:

[Route("consulta/clientePorNome/{nome:alpha}")]

Sources:

  

link

     

link

    
06.11.2015 / 13:50