【发布时间】:2021-01-26 23:52:18
【问题描述】:
我想知道如何从数据库中的 Alerts 表中删除所有行。我将 ASP.NET Core 与 Entity Framework 结合使用。当我现在单击删除按钮时,我会收到错误 404(未找到)
任何帮助将不胜感激!
控制器
private readonly FrontlineDbContext _dbContext;
public class AlertsController : Controller{
public AlertsController( FrontlineDbContext dbContext)
{
_dbContext = dbContext;
}
[HttpPost]
[Authorize(Roles = "Admin")]
public void Delete()
{
var alertsList = _dbContext.Alerts.ToList();
_dbContext.Alerts.RemoveRange(alertsList);
_dbContext.SaveChanges();
//displays a success message after updating, as stated in the Shared>_Layout
TempData["message"] = $"Alerts have been deleted";
}
}
警报视图
<h1>Alerts Overview</h1>
<a asp-action="Delete"> Delete Alerts</a>
【问题讨论】:
-
我认为您需要添加
asp-controller属性。(asp-controller="Alerts") 如果锚呈现任何内容,它可能指向“/Delete”而不是“/Alerts/Delete”结果为 405。 -
添加
asp-controller="alerts"并没有什么不同。它指向 /Alerts/Delete 但它仍然给出 405 错误 -
我相信你需要用 [HttpDelete] 来装饰你的方法。 405 is not Not Found,是Method Not Allowed,表示功能配置不正确。
标签: c# entity-framework asp.net-core