I am trying to get my page to post to my Web API controller, rather than my Area/Controller/Action. Here is what I have so far, I have tried using both Html.BeginForm and Ajax.Begin Form :
我试图让我的页面发布到我的Web API控制器,而不是我的区域/控制器/动作。这是我到目前为止,我已经尝试使用Html.BeginForm和Ajax.Begin表单:
@using (Ajax.BeginForm("", "", null, new AjaxOptions { HttpMethod = "POST", Url = "api/Standing" }, new { id = "frmStandingAdd", name = "frmStandingAdd" }))
@using (Html.BeginForm("", "api/Standing", FormMethod.Post, new { id = "frmStandingAdd", name = "frmStandingAdd" }))
But I cannot get either to post to the root ie http://domain/api/Standing
, instead both post to the Area ie http://domain/Areaname/api/Standing
. How do I get it to post correctly?
但我无法发送到根,即http://域/ api / Standing,而是发布到区域,即http://域/ Areaname / api / Standing。如何让它正确发布?
Update: Here are my routes for the relevant area :
更新:以下是相关领域的路线:
public override string AreaName
{
get
{
return "Areaname";
}
}
public override void RegisterArea(AreaRegistrationContext context)
{
string defaultLocale = "en-US";
context.MapRoute(
"Areaname_default",
"{languageCode}/Areaname/{controller}/{action}/{id}",
new { languageCode = defaultLocale, controller = "Main", action = "Index", id = UrlParameter.Optional });
context.MapRoute(
"History",
"{languageCode}/Areaname/{controller}/{action}/{year}",
new { controller = "History", action = "Season", year = UrlParameter.Optional });
}
And my Web API routes :
我的Web API路由:
config.Routes.MapHttpRoute(
"DefaultApi",
"api/{controller}/{id}",
new { id = RouteParameter.Optional }
);
config.Routes.MapHttpRoute(
"DefaultApiWithAction",
"api/{controller}/{action}/{season}",
new { id = RouteParameter.Optional }
);
2 个解决方案
#1
10
You can explicitly tell the links to post to the root by including the leading slash:
您可以通过包含前导斜杠明确告诉链接发布到根目录:
@using (Ajax.BeginForm("", "", null, new AjaxOptions { HttpMethod = "POST", Url = "/api/Standing" }, new { id = "frmStandingAdd", name = "frmStandingAdd" }))
@using (Html.BeginForm("", "/api/Standing", FormMethod.Post, new { id = "frmStandingAdd", name = "frmStandingAdd" }))
#2
7
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.
您需要使用BeginRouteForm作为Web API路由的链接生成始终取决于路由名称。另外,请确保提供名为httproute的路由值,如下所示。
@using (Html.BeginRouteForm("DefaultApi", new { controller="Entries", httproute="true" }))
#1
10
You can explicitly tell the links to post to the root by including the leading slash:
您可以通过包含前导斜杠明确告诉链接发布到根目录:
@using (Ajax.BeginForm("", "", null, new AjaxOptions { HttpMethod = "POST", Url = "/api/Standing" }, new { id = "frmStandingAdd", name = "frmStandingAdd" }))
@using (Html.BeginForm("", "/api/Standing", FormMethod.Post, new { id = "frmStandingAdd", name = "frmStandingAdd" }))
#2
7
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.
您需要使用BeginRouteForm作为Web API路由的链接生成始终取决于路由名称。另外,请确保提供名为httproute的路由值,如下所示。
@using (Html.BeginRouteForm("DefaultApi", new { controller="Entries", httproute="true" }))