Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

221
Visualizações
Get element based on string

I am creating a web api using mvc 6. now i am trying to get a element from my db. the key in this table is a string (email address). I do not have access to this database so i cant change the key of this table.

Now when creating a demo webapi i was able to create a controller to extract items based on a key which was an int. But when trying to get a element by a string the program crashes.

    [Route("api/[controller]")]
    public class TodoController : Controller
    {    

        [HttpGet("{id:string}", Name = "GetByIdRoute")]
        public IActionResult GetById (string id)
        {
            var item = _items.FirstOrDefault(x => x.Id == id);
            if (item == null)
            {
                return HttpNotFound();
            }

            return new ObjectResult(item);
        }
   }

when trying to access this path (example.com/api/Todo/key) key being the string i get an exception in my startup.cs

the exception in the browser reads:

System.InvalidOperationException The constraint entry 'id' - 'string' on the route 'api/Todo/{id:string}' could not be resolved by the constraint resolver of type 'DefaultInlineConstraintResolver'.

the part of the startup.cs where the code breaks is:

// Add MVC to the request pipeline.
            app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "default",
                    template: "{controller}/{action}/{id?}",
                    defaults: new { controller = "Home", action = "Index" });   

            });

i cant seem to figure out why i ain't allowed to get a item by a key which is a string. is this even possible and if so what am i doing wrong?

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

Just remove :string. You're not really constraining the value of the id anyway - it's already a string in the URL.

This fairly old blog post lists the available constraints - and you can see there's no :string constraint, because you don't need there to be.

The constraints are used to give "more specific" constraints a priority - e.g. "If that part of the URL is the string representation of a DateTime, use this route" - but as everything is a string (in a URL), there's nothing that a constraint of :string would make it more specific than, if you see what I mean.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda