-1

I need some help right there.

I'm using ASP.NET Core 3.1 MVC, all is going well, but not for a specific action.

I have several pages : Start -> Index -> AddUser.

I navigate between my pages thanks to <button>s.

Index.cshtml :

<form asp-controller="Home" asp-action="AddUser" asp-route-GB="@ViewBag.GB">
      <button type="submit" class="btn btn-success">Add</button>
</form>

HomeController.cs :

[HttpGet]
public ActionResult Start(string GB) {
  if (string.IsNullOrEmpty(GB)) {
    return RedirectPermanent("Error");
  }
  else {
    return View();
  }
}
[HttpGet]
public ActionResult Index(string GB) {
  if (string.IsNullOrEmpty(GB)) {
    return RedirectPermanent("Error");
  }
  else {
    return View();
  }
}
[HttpGet]
public ActionResult AddUser(string GB) {
  if (string.IsNullOrEmpty(GB)) {
    return RedirectPermanent("Error");
  }
  else {
    return View();
  }
}
public ActionResult Error() {
  return View();
}

URL results :

https://localhost:XXXXX/Home/Start?GB=1 -> Is going well.

https://localhost:XXXXX/Home/Index?GB=1 -> Is going well.

https://localhost:XXXXX/Home/AddUser?GB=1 -> HTTP ERROR 405

BUT ! If I add a slash to this last one : https://localhost:XXXXX/Home/AddUser/?GB=1 -> Is going well.

What is going on ? I don't want to add a slash on my browser to make it work...

Hashka
  • 111
  • 7
  • can you show your AddUser action pls? – Serge Feb 23 '21 at 13:40
  • Do you have multiple AddUser* routes? If so try experimenting by renaming the route name and check why the router can't identify this route correctly. You could make it more explicit by adding the route attribute [HttpGet("AddUser?GB={gb})]" if you didn't do that already. But with the information provided there is no way to answer why the route is not uniquely matched to an action. – verbedr Feb 23 '21 at 13:44
  • Now you got my entire HomeController.cs :) – Hashka Feb 23 '21 at 13:47

2 Answers2

0

First thing for while add or create api, it must be POST type not GET. Try to use [HttpPost] attribute. It might solve your issue.

  • Well, actually, it works in fact. But I don't get it, I'm not "posting" anything, I just want to GET a page. Just like Index or Start... – Hashka Feb 23 '21 at 14:41
0

Ok I got it, basically

  • <a> supports [HttpGet]
  • <button> + <form> supports [HttpPost]

If you want to use [HttpGet] then :

<a class="btn btn-success" asp-controller="Home" asp-action="AddUser" asp-route-GB="@ViewBag.GB">
    Next Step
</a>

If you want to use [HttpPost] then :

<form asp-controller="Home" asp-action="AddUser" asp-route-GB="@ViewBag.GB">
    <button type="submit" class="btn btn-success">Next Step</button>
</form>
Hashka
  • 111
  • 7