c# - Html.BeginForm 路由到 Web Api

标签 c# html asp.net-mvc asp.net-web-api

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

相关文章:

c# - 在 Visual Studio C# Express 2010 中调试 Nunit 测试

javascript - 如何设置页面上的CKEDitor实例始终为富文本编辑器

Javascript - 更改没有值的输入元素的内容

html - 在顶部对齐表格数据

c# - 从 ASP.NET 标识中删除 `AspNetUserLogins` 表

asp.net-mvc - 返回excel文件而不将其保存在 Controller 内的服务器中

c# - 我们如何确定 DateTime.Now 的 "quantum",即刻度之间的最小间隔?

c# - 在 ASP.NET Core 中启动 BackgroundService 的正确方法

jquery - 更改背景图像 css

asp.net-mvc - 有没有办法让 OutputCache 忽略 asp.net mvc 中的母版页?