繁体   English   中英

在 asp.net mvc Core 2.1 中调用 POST 方法时出现错误 404

[英]Error 404 when calling POST method in asp.net mvc Core 2.1

我正在开发我的第一个 MVC Core 2.1 应用程序并遇到问题(我猜是路由问题)。 调用如下所示的 POST 方法时:

    [HttpPost]
    public RedirectToActionResult Remove(int id)
    {
        Product p = _repository.Products.Where(x => x.Id == id).FirstOrDefault();
        _repository.Products.Remove(p);
        _repository.SaveChanges();
        return RedirectToAction("Index");
    }

我收到错误 404 页面未找到。 下面的标签位于发送请求的部分视图中:

<a method="POST" asp-controller="Product" asp-action="Remove" asp-route-id="@Model.Id" class="btn btn-danger">Delete</a>

它生成“ https://localhost:44398/Product/Remove/3 ”(在 id 为 3 的产品上),这似乎是匹配的

我正在使用默认路由

app.UseMvc(routes =>
        {
            routes.MapRoute(
                name: "default",
                template: "{controller=Home}/{action=Index}/{id?}");
        });

另外,如果我将相同的方法从 POST 更改为 get(摆脱数据库代码),我就可以访问它

    [HttpGet]
    public RedirectToActionResult Remove(int id)
    {

        return RedirectToAction("Index");
    }

我敢打赌我犯了一些愚蠢的错误,但我坚持下去,找不到答案。 任何帮助将不胜感激!

首先,如果您只是通过查询字符串传递一个 Id,为什么还需要 Post 呢?

另外,如果我没记错的话,你需要在调用 post 动词时设置 Content-Type 标题。

您可以尝试如下 -

<form method="post">
        <input name="id" type="text" value="2" />
        <button type="submit" asp-controller="Product" asp-action="Remove">Click Me </button>
</form>

这是使用表单 POST 将数据传递给操作的一种方法。 Model 绑定自动将表单中的数据绑定到操作参数。 输入标签的名称用于动态绑定数据。

我希望这有帮助。

HTTP POST请求在消息正文中从客户端(浏览器)向服务器提供附加数据。 相比之下, GET请求包括 URL 中的所有必需数据。 HTML中的Forms可以通过在元素。 指定的方法确定表单数据如何提交到服务器。 当方法为 GET 时,所有表单数据都被编码到 URL 中,作为查询字符串参数附加到操作 URL 中。 使用 POST,表单数据出现在 HTTP 请求的消息正文中。

有关 Get 和 Post 之间差异的更多详细信息,请参阅以下链接: https://stackoverflow.com/a/3477374/10201850

https://www.diffen.com/difference/GET-vs-POST-HTTP-Requests

一件事:如果您正在使用持久数据并想要删除他的主键作为 FK 在另一个表中的 object,您必须进行逻辑擦除,这就是您无法删除的原因,如果不是您的问题:I´我也是新手,我正在研究 .NET Core 3,所以我的路线看起来像:

 app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllerRoute(
                name: null,
                pattern: "{category}/Page{productPage:int}",
                defaults: new { controller = "Product", action = "List" }
            );
            endpoints.MapControllerRoute(
                name: null,
                pattern: "Page{productPage:int}",
                defaults: new
                {
                    controller = "Product",
                    action = "List",
                    productPage = 1
                }
                 ...
            );
// And my default:
            endpoints.MapControllerRoute("default", "{controller=Product}/{action=List}/{id?}");

这是我的一个项目的表格:

<form asp-action="Delete" method="post">
                    <a asp-action="Edit" class="btn btn-sm btn-warning"
                       asp-route-productId="@item.ProductID">
                        Edit
                    </a>
                    <input type="hidden" name="ProductID" value="@item.ProductID" />
                    <button type="submit" class="btn btn-danger btn-sm">
                        Delete
                    </button>
                </form>

这里有一个区别:我的: asp-route-productId="@item.ProductID"你的: asp-route-id="@Model.Id"你怎么称呼它?

这是我的编辑方法:

 [HttpPost]
        public IActionResult Delete(int productId)
        {
            Product deletedProduct = repository.DeleteProduct(productId);
            if(deletedProduct != null)
            {
                TempData["message"] = $"{deletedProduct.Name} ha sido borrado";
            }
            return RedirectToAction("Index");
        }
    }

最后一个电话:

public Product DeleteProduct(int productID)
        {
            Product dbEntry = context.Products
                .FirstOrDefault(p => p.ProductID == productID);
            if(dbEntry != null)
            {
                context.Products.Remove(dbEntry);
                context.SaveChanges();
            }
            return dbEntry;
        }

您可以尝试:更改为 IActionResult 而不是 RedirectToActionResult。

如果之前工作正常,将 web 配置文件重新发送到服务器,它将重置应用程序。 我猜在发布 web 配置文件应该是最新文件。

暂无
暂无

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

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