【问题标题】:How to Implement HttpDelete and HttpPut in Asp.Net MVC Core 3.1 c#. Resolve HTTP ERROR 405?如何在 Asp.Net MVC Core 3.1 c# 中实现 HttpDelete 和 HttpPut。解决 HTTP 错误 405?
【发布时间】:2021-03-27 00:23:54
【问题描述】:

技术信息: 框架 = Asp.Net Core 3.1 IDE = VisualStudio 2019

问题: 我有一个带有更新和删除操作方法的控制器。我有 UpdateView 和 DeleteView 从我需要重定向到相应的控制器。我已经实现了一个可以提交表单的按钮。我仍然面临 HTTP ERROR 405PUT & DELETE 相关的问题。有人可以帮我解决这个问题。提前致谢
控制器:

    [HttpPut]
    [ActionName("ModifyEmployee")]
    public IActionResult ModifyEmployee(int employeeId, Malips.Data.Employee employee)
    {
        if (ModelState.IsValid)
        {
            Malips.Data.Employee employeeDetail = _hrService.EmployeeSystem.UpdateEmployee(employee);
            return View("GetEmployee", employeeDetail);
        }
        return View();
    }

    [HttpDelete]
    public IActionResult DeleteEmployee(int employeeId)
    {
        _hrService.EmployeeSystem.DeleteEmployee(employeeId);
        return View("Index");
    }

更新视图:

@model Employee
@{
    ViewBag.Title = "Modify Employee";
}
<div>
    <form asp-controller="Hr" asp-action="ModifyEmployee" asp-route-employeeId="@Model.EmpId">
        <div class="form-group">
            <div asp-validation-summary="All" class="text-danger">
            </div>
        </div>
        @Html.HiddenFor(e => e.EmpId)
        <div class="form-group row">
            <label asp-for="FirstName" class="col-sm-2">First Name</label>
            <input asp-for="FirstName" class="col-sm-10" />
            <span asp-validation-for="FirstName" class="text-danger"></span>
        </div>
        <button type="submit" class="btn btn-info">Update</button>
    </form>
</div>

删除视图:

<form asp-controller="Hr" asp-action="DeleteEmployee" asp-route-employeeId="@Model.EmpId">
    <div class="row card" style="width: 18rem;">
        <div class="card-body">
            <label hidden="hidden">@Model.EmpId</label>
            <h5 class="card-title">@Model.FirstName  @Model.LastName</h5>
            <p class="card-text">@Model.Salary </p>
            <button type="submit" class="btn btn-danger">Delete</button>
        </div>
    </div>
</form>

【问题讨论】:

  • 嗨@Joseph Nannepaga,有关于这个案例的最新消息吗?
  • 嗨@Yinqiu,很少有人建议使用Ajax 调用。但是,我正在寻找完美的解决方案,即没有 Ajax nd 仅使用 Razor lib。
  • 嗨@Joseph Nannepaga,我认为使用httppost 比httpput 和httpdelete 更好。

标签: asp.net-core .net-core asp.net-core-mvc asp.net-core-3.1


【解决方案1】:

当前的 HTML5 不支持表单中的 PUT 或 DELETE。您只能将它与 ajax 或 httpclient 一起使用。或者,如果可能,您可以尝试@Html.BeginForm 剃须刀页面模板。 @Html.BeginForm 有发布方法选择。

现在从您的操作属性中删除 [ActionName("ModifyEmployee")]、[httpput] 和 [httpdelete]。 并改变

public IActionResult ModifyEmployee(int employeeId, Malips.Data.Employee employee)

到:

public IActionResult ModifyEmployee(Employee employee)

因为您不使用也不需要emploeeId。并从 ModifyEmployee 视图中删除 asp-route-employeeId="@Model.EmpId"。

【讨论】:

  • 1.如果我们删除动作动词,它们默认会变成 Post,所以实际的实现是错误的。 2.参数绑定没有问题。无需删除 Modify (int employeeId, Malips.Data.Employee employee) 中的第一个参数,因为它是我们产品的标准更新 URL 表示法。我只需要在我的项目中实现 HttpPut 和 HttpDelete。
  • 只有学生使用 Post、Put ...等。它只适用于具有经典 CRUDE 的教科书。在现实生活中,profies 不会使用它们中的任何一个,因为真正的控制器可以有数百个动作。
【解决方案2】:

就像@Sergey 说的,你可以将它与ajax 一起使用。下面是一个工作演示。

更新视图:

    <div>
    <form id="update" asp-controller="Hr" asp-action="ModifyEmployee">
        <div class="form-group">
            <div asp-validation-summary="All" class="text-danger">
            </div>
        </div>
        @Html.HiddenFor(e => e.EmpId)
        <div class="form-group row">
            <label asp-for="FirstName" class="col-sm-2">First Name</label>
            <input asp-for="FirstName" class="col-sm-10" />
            <span asp-validation-for="FirstName" class="text-danger"></span>
        </div>
        <div class="form-group row">
            <label asp-for="LastName" class="col-sm-2">First Name</label>
            <input asp-for="LastName" class="col-sm-10" />
            <span asp-validation-for="LastName" class="text-danger"></span>
        </div>
        <button type="submit" id="submit" class="btn btn-info">Update</button>
    </form>
</div>
@section scripts
{
    <script>
        $("#submit").click(function (e) {
        e.preventDefault();
        var data = $('#update').serialize();
        $.ajax({
            type: "PUT",
            url: "/hr/Modifyemployee",
            data: data,
            success: function (response) {
                window.location.href = response.redirectToUrl;
            }
        });
    })
    </script>
}

修改员工操作

 [HttpPut]
 [ActionName("ModifyEmployee")]
 //remember add this.
 [ValidateAntiForgeryToken]
 public async Task<IActionResult> ModifyEmployee(Employee employee)
 {
        //....
        return new JsonResult(new { redirectToUrl = Url.Action("Index", "Hr") });
 }

删除视图:

 <div>
    <form id="delete" asp-controller="Hr" asp-action="DeleteEmployee" asp-route-employeeId="@Model.EmpId">
        <div class="row card" style="width: 18rem;">
            <div class="card-body">
                <label hidden="hidden">@Model.EmpId</label>
                <h5 class="card-title">@Model.FirstName  @Model.LastName</h5>
                <button type="submit" id="submit" class="btn btn-danger">Delete</button>
            </div>
        </div>
    </form>
</div>
@section scripts
{
    $("#submit").click(function (e) {
        e.preventDefault();
            $.ajax({
            type: "delete",
            url: "/hr/DeleteEmployee?id=" + @Model.EmpId,
            success: function (response) {
                window.location.href = response.redirectToUrl;
            }
        });
    })
    </script>
}

删除员工操作

[HttpDelete]
public async Task<IActionResult> DeleteEmployee(int id)
{
   //......
   return new JsonResult(new { redirectToUrl = Url.Action("Index", "hr") });
}

测试结果:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-05-25
    • 2021-12-02
    • 1970-01-01
    • 2014-11-23
    • 2014-11-10
    • 2021-11-29
    • 2010-12-18
    • 1970-01-01
    相关资源
    最近更新 更多