【问题标题】:Getting 404 for HttpPost Action为 HttpPost 操作获取 404
【发布时间】:2021-03-26 12:21:19
【问题描述】:

我有一个在部分视图中显示的记录表,其中一些没有 ID 值。因此,在单击特定记录的编辑链接时,我尝试使用替代字段作为 ID。我不确定我是否可以合法地拥有两个 Post 操作方法,即使我使用不同的方法名称和参数。

目前,如果我单击带有 ID 的记录,则会调用正确的操作方法。如果我选择没有 ID 的记录(而不是使用唯一的“帐户”字符串 ID),我会得到 404。

RouteConfig:

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
    routes.MapMvcAttributeRoutes();
    routes.MapRoute(
        name: "Default",
        url: "{controller}/{action}/{id}",
        defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );
}

局部视图:

...
<td>
    @if (item.ID != null)
    {
        @Html.ActionLink("Edit", "EditBudget", new { id = item.ID })
    }
    else if (item.Account != null)
    {
        @Html.ActionLink("Edit", "EditAccountBudget", new { account = item.Account })
    }
</td>

预算控制器:

// POST: Budgets/Edit/5
[Route("edit/{id?}")]
[HttpPost]
public ActionResult EditBudget(int? id = null, FormCollection collection = null)
{
    ...
    // Responding correctly for URL: http://localhost:4007/edit/19
}

[Route("editaccountbudget/{account}")]
[HttpPost]
public ActionResult EditAccountBudget(string account)
{
    ...
    // Getting 404 for URL: http://localhost:4007/editaccountbudget/6000..130
}

【问题讨论】:

  • [Route("budgets/@Html.ActionLink("Edit" 不兼容,我猜。
  • 已更新。仍然得到 404。
  • 试试这个[Route("edit/editaccountbudget/{account}")]
  • @IrishChieftain:您似乎没有在这里使用帖子。尝试删除[HttpPost] 属性。

标签: c# asp.net asp.net-mvc model-view-controller asp.net-mvc-5


【解决方案1】:

假设 EditBudget 是您的控制器名称,您可以更改 您的路线以避免混淆(或保持原样,因为属性路线将被忽略)并从您的操作中删除 [POST]:

[Route("~EditBudget/EditAccountBudget/{account}")]

也改变:

        @Html.ActionLink("Edit", "EditAccountBudget", new new { account = item.Account })

收件人:

        @Html.ActionLink("EditAccountBudget", "EditBudget", new { account = item.Account })
   

如果您使用剃须刀页面模板控件,您需要根据您的路由映射同时拥有路由的控制器和操作部分。如果您使用 ajax 或 httpclient,您可以使用任何语法。

【讨论】:

  • 好奇为什么路由属性会被忽略? ActionLink 的第一个参数是该链接的文本,所以这并没有真正改变任何东西。
  • @Html.ActionLink 生成带有控制器和动作名称而不是路由的 标记。
  • 在这种情况下,您似乎是正确的。但是从我所见,可以选择在 API 中传递路由。我找到了解决方案并将发布答案。
【解决方案2】:

ActionLink 呈现常规锚 () 标记,因此它只执行 GET 而不是 POST。如果你想 POST 值,你需要使用一个实际的表单(构建你自己的标签,或者使用 Html.BeginForm() ),然后在该表单的范围内包含一个提交按钮。

【讨论】:

  • 在 GET 上+1。这是片面的看法。该链接将用户带到编辑表单,该表单将具有自己的表单标签。
  • @IrishChieftain:如果有帮助,我很高兴。我在我的应用程序中遇到了同样的问题。
【解决方案3】:

当您在 ActionLink 中使用方法名称时,您的 BudgetsController 应该如下所示没有 HttpPost 属性没有 Route 属性。如果您愿意,可以使用 HttpGet 属性。

EditBudget 方法中也不需要 FormCollection 集合 参数。你不会得到任何东西,因为它的 Get not Post。

public ActionResult EditBudget(int? id = null)
{
}

public ActionResult EditAccountBudget(string account)
{
}

【讨论】:

  • 对 GET 进行投票。我最终能够让它工作,并将发布我的答案。
【解决方案4】:

正如一些人所指出的,这是一个 GET 请求。如果 ID 为空,我必须传递模型,因为我需要的不仅仅是帐户 ID 来构建数据库查询。

局部视图:

@if (item.ID != null)
{
    @Html.ActionLink("Edit", "EditBudget", new { id = item.ID })
}
else if (item.Account != null)
{
    @Html.ActionLink("Edit", "EditBudget", new { account = item.Account,
        SelectedDepartment = item.SelectedDepartment, SelectedYear = item.SelectedYear })
}

预算控制器:

// GET
public ActionResult EditBudget(int? id, BudgetsViewModel model)
{
    repo = new BudgetDemoRepository();
    if (id != null)
    {
        // Pass id to repository class
    }
    else
    {
        // Pass account and other params to repository class

    }

    return View(...);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-01-05
    • 1970-01-01
    • 1970-01-01
    • 2014-10-08
    • 2012-07-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多