C# HttpDeleteAttribute不';行不通

C# HttpDeleteAttribute不';行不通,c#,html,http,asp.net-mvc-4,razor,C#,Html,Http,Asp.net Mvc 4,Razor,在我的控制器中,我尝试了类似的方法: [HttpDelete, ActionName("Delete")] [ValidateAntiForgeryToken] public ActionResult DeleteConfirmed(int id) { Movie movie = db.Movies.Find(id); db.Movies.Remove(movie); db.SaveChanges();

在我的控制器中,我尝试了类似的方法:

    [HttpDelete, ActionName("Delete")]
    [ValidateAntiForgeryToken]
    public ActionResult DeleteConfirmed(int id)
    {
        Movie movie = db.Movies.Find(id);
        db.Movies.Remove(movie);
        db.SaveChanges();
        return RedirectToAction("Index");
    }
单击后,steering不会访问上述方法。 如果少了什么?可能问题出在html代码的视图中。我使用标准表格,例如:

@using (Html.BeginForm()) {
    @Html.AntiForgeryToken()
    <p>
        <input type="submit" value="Delete" formmethod="delete"/> |
        @Html.ActionLink("Back to List", "Index")
    </p>
}
@使用(Html.BeginForm()){
@Html.AntiForgeryToken()

|
@ActionLink(“返回列表”、“索引”)

}
我也尝试了
Html.BeginForm(“Delete”、“Action”、FormMethod.Delete)
,但IntelliSense不支持它(它表明只有FormMethod.Get和FormMethod.Post)


据我所知,Delete方法正是用来操作要从数据库中删除的对象的。如果我是对的,你能解释一下吗?如果没有,为什么?如果是-请告诉我如何以良好的方式实现该方法。

我认为没有必要将您的操作限制为针对您的场景的
HttpDelete
。改用
HttpPost
,一切正常

如果您决定使用该方法,那么您的表单应该使用
HttpDelete
将请求发送到服务器


HttpDelete
更适合REST Api,我不认为在您的情况下使用该限制会有什么好处。

对于您的场景,我认为没有必要将您的操作限制为
HttpDelete
。改用
HttpPost
,一切正常

如果您决定使用该方法,那么您的表单应该使用
HttpDelete
将请求发送到服务器

HttpDelete
更适用于REST Api,我看不出在您的情况下使用该限制会有什么好处。

在您的
BeginForm()
调用中添加一个:

@using (Html.BeginForm()) {
    @Html.HttpMethodOverride(HttpVerbs.Delete)
    @Html.AntiForgeryToken()
    <p>
        <input type="submit" value="Delete" formmethod="delete"/> |
        @Html.ActionLink("Back to List", "Index")
    </p>
}
@使用(Html.BeginForm()){
@httpmethodverride(HttpVerbs.Delete)
@Html.AntiForgeryToken()

|
@ActionLink(“返回列表”、“索引”)

}
在您的
BeginForm()调用中添加一个:

@using (Html.BeginForm()) {
    @Html.HttpMethodOverride(HttpVerbs.Delete)
    @Html.AntiForgeryToken()
    <p>
        <input type="submit" value="Delete" formmethod="delete"/> |
        @Html.ActionLink("Back to List", "Index")
    </p>
}
@使用(Html.BeginForm()){
@httpmethodverride(HttpVerbs.Delete)
@Html.AntiForgeryToken()

|
@ActionLink(“返回列表”、“索引”)

}
我知道,这是脚手架系统生成的标准MVC4,但我对如何正确使用MVC4中的HttpDelete方法感兴趣。@pt12lol:如果只是为了测试目的,您可以使用RGraham解决方案,但要考虑到您可能使事情变得比需要的更复杂。我明白了。非常感谢您的解释:)我知道,这是脚手架系统生成的标准MVC4,但我对如何正确使用MVC4中的HttpDelete方法感兴趣。@pt12lol:如果只是为了测试目的,您可以使用RGraham解决方案,但要考虑到您可能使事情变得比必要的更复杂。我明白了。非常感谢您的解释:)