为 HttpPost 操作获取 404
Getting 404 for HttpPost Action
我在部分视图中显示了 table 条记录,其中一些没有 ID 值。因此,当单击特定记录的编辑 link 时,我尝试使用替代字段作为 ID。我不确定我是否可以合法地拥有两个 Post 操作方法,即使我使用不同的方法名称和参数也是如此。
目前,如果我单击具有 ID 的记录,则会调用正确的操作方法。如果我 select 没有 ID 的记录(而不是使用唯一的“帐户”字符串 ID),我会收到 404。
路由配置:
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
}
假设 EditBudget 是您的控制器名称,您可以更改
你的路线以避免混淆(或保持原样,因为属性路线将被忽略)并从你的行动中删除 [POST] :
[Route("~EditBudget/EditAccountBudget/{account}")]
同时更改:
@Html.ActionLink("Edit", "EditAccountBudget", new new { account = item.Account })
收件人:
@Html.ActionLink("EditAccountBudget", "EditBudget", new { account = item.Account })
如果您使用 razor 页面模板控件,您需要根据您的路由映射同时拥有路由的控制器和操作部分。如果您使用 ajax 或 httpclient,您可以使用任何语法的路由。
ActionLink
呈现常规锚点 () 标记,因此它仅 GET 而不是 POST。如果你想要 POST 值,你需要使用一个实际的表单(要么构建你自己的标签,要么使用 Html.BeginForm()
)然后包含在该表单的范围内提交按钮。
您的 BudgetsController 应该如下所示 没有 HttpPost 属性 和 没有路由属性 因为您正在使用方法名称动作链接。如果您愿意,可以使用 HttpGet 属性。
也不需要在 EditBudget 方法中使用 FormCollection 集合 参数。你不会得到任何东西,因为它的 Get 不是 Post.
public ActionResult EditBudget(int? id = null)
{
}
public ActionResult EditAccountBudget(string account)
{
}
正如一些人所指出的,这是一个 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(...);
}
我在部分视图中显示了 table 条记录,其中一些没有 ID 值。因此,当单击特定记录的编辑 link 时,我尝试使用替代字段作为 ID。我不确定我是否可以合法地拥有两个 Post 操作方法,即使我使用不同的方法名称和参数也是如此。
目前,如果我单击具有 ID 的记录,则会调用正确的操作方法。如果我 select 没有 ID 的记录(而不是使用唯一的“帐户”字符串 ID),我会收到 404。
路由配置:
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
}
假设 EditBudget 是您的控制器名称,您可以更改 你的路线以避免混淆(或保持原样,因为属性路线将被忽略)并从你的行动中删除 [POST] :
[Route("~EditBudget/EditAccountBudget/{account}")]
同时更改:
@Html.ActionLink("Edit", "EditAccountBudget", new new { account = item.Account })
收件人:
@Html.ActionLink("EditAccountBudget", "EditBudget", new { account = item.Account })
如果您使用 razor 页面模板控件,您需要根据您的路由映射同时拥有路由的控制器和操作部分。如果您使用 ajax 或 httpclient,您可以使用任何语法的路由。
ActionLink
呈现常规锚点 () 标记,因此它仅 GET 而不是 POST。如果你想要 POST 值,你需要使用一个实际的表单(要么构建你自己的标签,要么使用 Html.BeginForm()
)然后包含在该表单的范围内提交按钮。
您的 BudgetsController 应该如下所示 没有 HttpPost 属性 和 没有路由属性 因为您正在使用方法名称动作链接。如果您愿意,可以使用 HttpGet 属性。
也不需要在 EditBudget 方法中使用 FormCollection 集合 参数。你不会得到任何东西,因为它的 Get 不是 Post.
public ActionResult EditBudget(int? id = null)
{
}
public ActionResult EditAccountBudget(string account)
{
}
正如一些人所指出的,这是一个 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(...);
}