Asp.net mvc 为什么MVC操作方法选择器没有选择我的HttpPut操作?

Asp.net mvc 为什么MVC操作方法选择器没有选择我的HttpPut操作?,asp.net-mvc,routing,http-post,http-put,actionmethod,Asp.net Mvc,Routing,Http Post,Http Put,Actionmethod,考虑到以下路线: context.MapRoute(null, "widgets", new { controller = "Widgets", action = "Add" }, new { httpMethod = new HttpMethodConstraint("PUT") }); context.MapRoute(null, "widgets", new { controller = "Widgets", action = "Add" }, n

考虑到以下路线:

context.MapRoute(null, "widgets", 
    new { controller = "Widgets", action = "Add" }, 
    new { httpMethod = new HttpMethodConstraint("PUT") });
context.MapRoute(null, "widgets", 
    new { controller = "Widgets", action = "Add" }, 
    new { httpMethod = new HttpMethodConstraint("PUT", "POST") });
以及以下控制器:

public class WidgetsController
{
    [HttpPut]
    public ActionResult Add(WidgetForm model)
    {
        return DoStuff(); // code here doesn't matter
    }
}
以及呈现以下形式的视图(使用
HtmlHelper@Html.httpmethodverride(HttpVerbs.Put)


这似乎不对……是吗?在我看来,我应该能够在没有POST约束的情况下完成这项工作。操作方法没有用HttpPost修饰,那么为什么需要POST约束呢?

是的。深入了解一下这在MVC管道中的工作原理,它实际上是MVC(ActionMethodSelectorAttribute、ActionInvoker、RedirectToRoute)处理此而不是RouteModule


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

我认为我不能使用PUT作为表单方法。我使用@Html.BeginForm来呈现表单,其参数采用Post或Get枚举。http规范中是否有关于浏览器表单仅支持Post和Get的内容?深入了解这在MVC管道中的工作原理,实际上是MVC(ActionMethodSelectorAttribute、ActionInvoker、RedirectToRoute)处理这个问题,而不是RouteModule。我知道这是操作方法选择器(请参阅原始问题标题)。所以我应该同时使用POST和PUT?如果不添加“POST”约束,则路由将不匹配。对于路由模块,这是一个POST请求,而不是PUT。
context.MapRoute(null, "widgets", 
    new { controller = "Widgets", action = "Add" }, 
    new { httpMethod = new HttpMethodConstraint("PUT", "POST") });