简体   繁体   English

ASP .NET中的MVC操作会引发404错误

[英]MVC action in ASP .NET throws 404 error

I have to add a delete method to an existing application designed with ASP .NET, the thing is that I have 0 experience with. 我必须向使用ASP .NET设计的现有应用程序中添加一个delete方法,事实是我有0个经验。 Here is what I've done : 这是我所做的:

CONTROLLER 控制器

[HttpPost, Authorize(Roles = "Admin")]
public ActionResult TypeLotRemove(Guid id)
{
    TypeLot typeLot = _repository.GetTypeLot(id);

    //On supprime le type de lot à la base.
    _repository.RemoveTypeLot(typeLot);
    return RedirectToAction("TypeLot");
}

REPOSITORY 仓库

public TypeLot RemoveTypeLot(TypeLot typeLot)
{
    _entities.RemoveToTypeLot(typeLot);
    _entities.SaveChanges();
    return typeLot;
}

Designer.cs Designer.cs

[global::System.CodeDom.Compiler.GeneratedCode("System.Data.Entity.Design.EntityClassGenerator", "4.0.0.0")]
public void RemoveToTypeLot(TypeLot typeLot)
{
    base.DeleteObject(typeLot);
}

VIEW 视图

<%= Html.ActionLink("Supprimer","TypeLotRemove", new { id = item.ID} )%>

When I click on the link, I'm redirected to TypeLotRemove/420c039f-516e-40c5-916f-6da222eb50ae so I have a 404 error (normal cause I don't have TypeLotRemove file) and nothing is done. 当我单击链接时,我被重定向到TypeLotRemove/420c039f-516e-40c5-916f-6da222eb50ae所以我遇到404错误(正常原因是我没有TypeLotRemove文件)并且什么也没做。 What did I missed ? 我错过了什么?

The issue is that you are just generating a link to the ActionMethod , when you have decorated the ActionMethod with the [HttpPost] verb. 问题是,用[HttpPost]动词修饰ActionMethod时,您只是生成到ActionMethod的链接。 This means that this ActionMethod is only accessible via a POST request. 这意味着只能通过POST请求访问此ActionMethod

Using [HttpPost] makes sense in this case so just do this in your View: 在这种情况下,使用[HttpPost]是有意义的,因此只需在您的View中执行以下操作:

@using(Html.BeginForm("TypeLotRemove", "Controller", FormMethod.Post)
{
   // Your form and submit
}

Also pointed out by mstaessen in the comments you are attempting to access the URL /TypeLotRemove/1 again this is a GET request but the parameter you are passing (in this case 1 ) is not a GUID that you require in your ActionMethod mstaessen也在注释中指出,您再次尝试访问URL /TypeLotRemove/1 ,这是一个GET请求,但您传递的参数(在本例中为1 )不是您在ActionMethod所需的GUID

更改HttPostHttpGet的方法TypeLotRemove

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM