Change /controller? id=1 URL to /controller/1

Asked

Viewed 55 times

1

In my view I have two buttons that pass parameters via GET to my controller index, the problem is that the URL does not stick to the traditional ? id=1 and I wanted to make it look like this: /controller/1

View:

@using (Html.BeginForm(FormMethod.Get))
        {
            <button type="submit" class="btn btn-primary" value="1" name="id">sites1</button>
            <button type="submit" class="btn btn-primary" value="2" name="id">sites2</button><br />

        }

Controller:

 public async Task<IActionResult> Index(int? id)
    {
        var listaDeSites = await _listaService.ListaDeSitesAsync(id);
        return View(listaDeSites);
    }
  • Could you post your Routeconfig class code? Thank you.

  • Ta in the default Asp.net core app.Usemvc(Routes => { Routes.Maproute( name: "default", template: "{controller=Home}/{action=Index}/{id? }"); });

  • ps.: Do you mean /Controller/1, /Controller/Action/1 or ./Action/1 ?

1 answer

2


You could put the following Annotation:

[HttpGet("{id}")]

being like this:

[HttpGet("{id}")]
public async Task<IActionResult> Index(int? id)
{
    var listaDeSites = await _listaService.ListaDeSitesAsync(id);
    return View(listaDeSites);
}

Browser other questions tagged

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