我试图让我的页面发布到我的 Web API Controller ,而不是我的区域/ Controller /操作。到目前为止,这是我所拥有的,我尝试同时使用 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/站着
。如何让它正确发布?
更新:这是我在相关区域的路线:
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" }))
关于c# - Html.BeginForm 路由到 Web Api,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16510873/