超文本标记语言BeginForm路由到Web Api
本文关键字:Web Api 路由 BeginForm 超文本标记语言 | 更新日期: 2023-09-27 18:13:17
我试图让我的页面张贴到我的Web API控制器,而不是我的区域/控制器/动作。这是我到目前为止所做的,我尝试过使用Html。BeginForm和Ajax。Begin Form:
@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" }))
但是我不能得到任何一个发布到根即http://domain/api/Standing
,而是两个发布到区域即http://domain/Areaname/api/Standing
。我如何得到它张贴正确?
更新:以下是我在相关区域的路线:
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 });
}
和我的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 }
);
您可以通过包含前导斜杠来显式地告诉链接要发布到根目录:
@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" }))
您需要使用BeginRouteForm
作为Web API路由的链接生成始终取决于路由名称。还要确保提供如下所示的名为httproute
的路由值。
@using (Html.BeginRouteForm("DefaultApi", new { controller="Entries", httproute="true" }))