在ASP.NET MVC Core 2.1中调用POST方法时出现错误404

我正在开发我的第一个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更改为获取(摆脱数据库代码),我将能够访问它

    [HttpGet]
    public RedirectToactionResult Remove(int id)
    {

        return RedirectToaction("Index");
    }

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

ruanhuibao 回答:在ASP.NET MVC Core 2.1中调用POST方法时出现错误404

首先,如果仅通过查询字符串传递一个ID,为什么还需要Post?

此外,如果我没记错的话,在调用帖子动词时,您需要设置Content-Type Headers。

,

您可以尝试以下操作-

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

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

我希望这会有所帮助。

,

HTTP POST 请求在消息正文中从客户端(浏览器)向服务器提供其他数据。相反, GET 请求在URL中包含所有必需的数据。 HTML表单可以通过在 元素中指定 method =“ POST” method =“ GET” (默认)来使用这两种方法。指定的方法确定如何将表单数据提交到服务器。当方法为GET时,所有表单数据都被编码为URL,并作为查询字符串参数附加到操作URL。使用POST,表单数据将显示在HTTP请求的消息正文中。

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

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

,

一件事:如果您正在使用持久数据,并且想以FK删除其主键位于另一个表中的对象,则必须进行逻辑擦除,这就是为什么您无法删除(如果不是这样的话)的原因: 我也是新手,并且正在.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" 你怎么称呼它?

这是我的Edit方法:

 [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配置文件中应该是最新文件。

本文链接:https://www.f2er.com/3121345.html

大家都在问