ASP NET Core Dynamic value "Route"

Asked

Viewed 41 times

0

Can the route parameter be dynamic? for example I want that when Uf is "sp" I put sao-paulo, and so on with the other states. Because then when I load the aciton it loads the url as I reported it. My problem is actually being able to rewrite the url by hiding the parameters. If anyone has any light I appreciate.

    [HttpGet]
    [Route("/sao-paulo")] <<<< (Esse Parametro tem como ser dinamico ????)
    public IActionResult EstadoSelecionado(string uf)
    {
        
        return View("Index_new", pessoa);
    }
  • if you pass the value between keys on the route, it takes the parameter value. for example: [Route("/{Uf}")]

  • @Marcosjunior Simply very, but thank you very much. Helped me too much, I had tried once like this, ams ai went to read the documentation and this parameter can not be null. Dude, strong vlw hug too

1 answer

0


Passing the parameter on the route between keys

[Route("{uf}")] //uf assumirá o valor para o parâmetro

Another detail you mentioned in the comment for cases where the value may come null. you can treat this by making another mapping to the same method.

example:

[HttpGet]
[Route("estados")] //se nao houver valor, uf será null
[Route("estados/{uf}")]
public IActionResult EstadoSelecionado(string uf)
{
    
    return View("Index_new", pessoa);
}

Note that I have added states to the route so that it does not conflict with other routes that do not have parameters.. if not your case, you can add a route in this way

[Route("/")]
  • Monster too much ! Helped me a lot I managed to finish my task. Obg from heart!

Browser other questions tagged

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