Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Html.begin路由到Web Api_C#_Html_Asp.net Mvc_Asp.net Web Api - Fatal编程技术网

C# Html.begin路由到Web Api

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

我试图让我的页面发布到我的Web API控制器,而不是我的区域/控制器/操作。到目前为止,我已经尝试使用Html.BeginForm和Ajax.BeginForm:

@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
,而是将两者都发布到该区域,即
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这实际上帮助了我。我已经找了很长时间了。