【发布时间】:2024-05-15 18:50:02
【问题描述】:
给定以下路线:
context.MapRoute(null, "widgets",
new { controller = "Widgets", action = "Add" },
new { httpMethod = new HttpMethodConstraint("PUT") });
还有以下控制器:
public class WidgetsController
{
[HttpPut]
public ActionResult Add(WidgetForm model)
{
return DoStuff(); // code here doesn't matter
}
}
以及呈现以下表单的视图(使用HtmlHelper.@Html.HttpMethodOverride(HttpVerbs.Put):
<form action="/widgets" method="post">
<!-- many form elements, then -->
<input name="X-HTTP-Method-Override" type="hidden" value="PUT" />
</form>
提交表单时,MVC动作方法选择器没有选择上述动作方法。如果我在左大括号上设置断点,它永远不会被命中。在浏览器中,它返回 404 页面(我相信这是默认的 ActionNotFound 行为)。
但是,操作方法选择器确实选择了具有以下路由的 Add HttpPut 方法:
context.MapRoute(null, "widgets",
new { controller = "Widgets", action = "Add" },
new { httpMethod = new HttpMethodConstraint("PUT", "POST") });
这似乎不对……是吗?在我看来,我应该能够在没有 POST 约束的情况下做到这一点。 action 方法没有用 HttpPost 修饰,那么为什么需要 POST 约束呢?
【问题讨论】:
标签: asp.net-mvc routing http-post http-put actionmethod