WebApi中的Html.BeginForm - 路由

Mic*_*per 1 asp.net asp.net-mvc asp.net-mvc-4 asp.net-web-api asp.net-web-api-routing

我有一个Web API项目作为我的解决方案的一部分(也包含一个MVC4项目),在Api项目中我试图将一个表单发布到Values控制器Post方法(从Api项目中的视图).

使用Html.BeginForm()Html.BeginForm("Post", "Values")发布,/Values/Post但我需要它去/api/Values/Post

知道我需要将哪些重载或设置发布到正确的位置?

我可以从fiddler中获得所有操作方法(例如localhost/api/values).

Kir*_*lla 6

您需要使用BeginRouteForm链接生成到Web API路由始终取决于路由名称.还要确保提供httproute如下所示的路由值.

@using (Html.BeginRouteForm("DefaultApi", new { controller="Values", httproute="true" }))
Run Code Online (Sandbox Code Playgroud)