1

How to make asp-action tag helper do http delete?

At the moment i have following code which doesn't work

<a class="btn" asp-action="Delete" asp-controller="Home" asp-route-accountKey="@Model.Item1.AccountKey" method="delete">Delete</a>

Controller

[HttpDelete]
public void Delete(string accountKey)
{

}

Without [HttpDelete] it enters the action method.

Kirk Larkin
  • 84,915
  • 16
  • 214
  • 203
Alexander
  • 17
  • 1
  • 4

1 Answers1

1

This is not an issue with the ASP.NET Core MVC asp-action Tag Helper - the problem is down to the fact that delete is not a supported method in HTML forms (see the method section here).

Although different browsers may handle it differently, Chrome just issues a GET request when it sees delete as the HTML form's method. When you remove [HttpDelete] from your Delete action, it defaults to GET (it's as though you'd added [HttpGet]), which is why the GET verb being used by Chrome now hits your Delete action.

In order to fix this, I suggest using the POST verb, which can be triggered using a method of post in your form and adding [HttpPost] to your delete action. Here's what it looks like:

HTML

<a
    class="btn"
    asp-action="Delete"
    asp-controller="Home"
    asp-route-accountKey="@Model.Item1.AccountKey"
    method="post">Delete</a>

C#

[HttpPost]
public void Delete(string accountKey) { }

Using post is preferred over using get for reasons cited by here.

Kirk Larkin
  • 84,915
  • 16
  • 214
  • 203