简体   繁体   English

为 HttpPost 操作获取 404

[英]Getting 404 for HttpPost Action

I have a table of records being displayed in a partial view and some of them have no ID values.我有一个记录表显示在部分视图中,其中一些没有 ID 值。 So I am trying to use an alternative field as an ID when clicking the Edit link for a particular record.因此,在单击特定记录的编辑链接时,我尝试使用替代字段作为 ID。 I'm not sure if I can legally have two Post action methods, even though I am using different methods names and params.我不确定我是否可以合法地拥有两个 Post 操作方法,即使我使用不同的方法名称和参数。

Currently, if I click on a record with an ID the correct action method gets called.目前,如果我单击带有 ID 的记录,则会调用正确的操作方法。 If I select a record with no ID (instead using an "account" string ID which is unique), I get a 404.如果我 select 没有 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 }
        );
}

Partial View:部分视图:

...
<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>

BudgetsController:预算控制器:

// 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
}

Assuming that EditBudget is your controller name, you can change your route to this to avoid confusing ( or leave as it is since the attribute route will be ignored) and remove [POST] from your action too:假设 EditBudget 是您的 controller 名称,您可以将路线更改为此以避免混淆(或保留原样,因为属性路线将被忽略)并从您的操作中删除 [POST] :

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

Also change:也改变:

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

To:至:

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

If you use razor pages template controls you need to have both controller and action parts of the route according your route mapping.如果您使用 razor 页面模板控件,您需要根据您的路线映射同时拥有 controller 和路线的动作部分。 If you use ajax or httpclient you can have any syntax of route.如果您使用 ajax 或 httpclient,您可以使用任何路由语法。

ActionLink renders a regular anchor (<a />) tag, so it only does GET not POST . ActionLink呈现一个常规的锚 (<a />) 标签,所以它只做GET而不是POST If you want to POST values, you need to use an actual form (either building your own tag, or using Html.BeginForm() ) and then include inside that form's scope a submit button.如果要发布值,则需要使用实际表单(构建您自己的标签,或使用Html.BeginForm Html.BeginForm() ),然后在该表单的 scope 中包含一个提交按钮。

Your BudgetsController should look like below without HttpPost Attribute and without Route Attribute as you are using method names in ActionLink.当您在 ActionLink 中使用方法名称时,您的 BudgetsController 应该如下所示,没有 HttpPost 属性Route 属性 You can use HttpGet attribute if you wish.如果您愿意,可以使用 HttpGet 属性。

Also no need of FormCollection collection parameter in EditBudget method. EditBudget 方法中也不需要FormCollection 集合参数。 You will not get anything as its Get not Post.你不会得到任何东西,因为它的 Get not Post。

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

public ActionResult EditAccountBudget(string account)
{
}

As some have pointed out, this is a GET request.正如一些人所指出的,这是一个 GET 请求。 If the ID was null, I had to pass the model because I needed more than the account ID to construct the database query.如果 ID 是 null,我必须通过 model,因为我需要的不仅仅是帐户 ID 来构建数据库查询。

Partial View:部分视图:

@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 })
}

BudgetsController:预算控制器:

// 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(...);
}

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

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