14

我试图让我的页面发布到我的 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" }))

但我无法将任何一个发布到根 ie http://domain/api/Standing,而是都发布到区域 ie 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 }
);
4

2 回答 2

12

您可以通过包含前导斜杠来明确告知要发布到根目录的链接:

@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" }))
于 2013-05-12T21:44:10.553 回答
8

您需要BeginRouteForm用作 Web API 路由的链接生成始终取决于路由名称。还要确保提供httproute如下调用的路由值。

@using (Html.BeginRouteForm("DefaultApi", new { controller="Entries", httproute="true" }))
于 2013-12-03T01:02:22.433 回答