I'm working on my first MVC Core 2.1 app and encountered problem (i guess with the routing). When calling a POST method shown below:
[HttpPost]
public RedirectToActionResult Remove(int id)
{
Product p = _repository.Products.Where(x => x.Id == id).FirstOrDefault();
_repository.Products.Remove(p);
_repository.SaveChanges();
return RedirectToAction("Index");
}
i get the error 404 page not found. Below tag located in a partial view which sends the request:
<a method="POST" asp-controller="Product" asp-action="Remove" asp-route-id="@Model.Id" class="btn btn-danger">Delete</a>
it generates "https://localhost:44398/Product/Remove/3" (on product with id 3) which seems to be matching
I'm using default routing
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
also, if i change this same method from POST to get (getting rid of the database code) i'm able to access it
[HttpGet]
public RedirectToActionResult Remove(int id)
{
return RedirectToAction("Index");
}
I bet I've made some stupid mistake, but I'm stuck with it and can't find the answer. Any help will be appreciated!