C# Html.begin路由到Web Api
我试图让我的页面发布到我的Web API控制器,而不是我的区域/控制器/操作。到目前为止,我已经尝试使用Html.BeginForm和Ajax.BeginForm:C# Html.begin路由到Web Api,c#,html,asp.net-mvc,asp.net-web-api,C#,Html,Asp.net Mvc,Asp.net Web Api,我试图让我的页面发布到我的Web API控制器,而不是我的区域/控制器/操作。到目前为止,我已经尝试使用Html.BeginForm和Ajax.BeginForm: @using (Ajax.BeginForm("", "", null, new AjaxOptions { HttpMethod = "POST", Url = "api/Standing" }, new { id = "frmStandingAdd", name = "frmStandingAdd" })) @using (H
@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" }))
但我无法将两者都发布到根iehttp://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" }))
请发布您的
路由
谢谢,Ajax.BeginForm有效,但Html.BeginForm无效,因此我使用Ajax.BeginFormHad删除Html.BeginForm中的第一个/api/Standing。Mvc似乎在默认情况下添加了一个,否则我会得到//api/standing这实际上帮助了我。我已经找了很长时间了。