为什么MVC操作方法选择器没有选择我的HttpPut操作



给定以下路径:

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约束的情况下做到这一点。操作方法没有用HttpPost修饰,那么为什么POST约束是必要的呢?

右侧。深入了解一下这在MVC管道中的工作方式,实际上是MVC(ActionMethodSelectorAttribute、ActionInvoker、RedirectToRoute)处理这一问题,而不是RouteModule。

所以在路由模块中,它仍然是一个"POST"请求,而不是一个"PUT"。

最新更新