Reputation: 1531
I have a Web API project as part of my solution (also containing an MVC4 project) and within the Api project I am trying to post a form to the Values controller Post method (from a view also within the Api project).
Using Html.BeginForm()
or Html.BeginForm("Post", "Values")
posts to /Values/Post
but I need it to go to /api/Values/Post
Any idea which overload or settings I need to post to the correct location?
I can hit all the action methods fine from fiddler (e.g. localhost/api/values).
Upvotes: 1
Views: 2519
Reputation: 57949
You would need to use BeginRouteForm
as link generation to Web API routes always depends on the route name. Also make sure to supply the route value called httproute
as below.
@using (Html.BeginRouteForm("DefaultApi", new { controller="Values", httproute="true" }))
Upvotes: 6
Reputation: 24526
The API controller uses a different route to the default. It's supposed to be consumed from JS (AJAX) rather than a real form post so there's no obvious support for it in HtmlHelpers. Try:
Html.BeginForm("values", "api")
This would trick it into thinking "values" is the action and "api" is the controller. "Post" is inferred from the http method.
Upvotes: 2