Html.BeginForm路由到网页API [英] Html.BeginForm routing to Web Api

查看:117
本文介绍了Html.BeginForm路由到网页API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让我的网页发布到我的Web API控制器,​​而不是我的领域/控制器/动作。这是我到目前为止,我已经尝试使用这两种Html.BeginForm和Ajax.Begin形式:

  @using(Ajax.BeginForm(,,空,新AjaxOptions {列举HTTPMethod =POST,URL =API /站在},{新的id = frmStandingAddNAME =frmStandingAdd}))@using(Html.BeginForm(,API /常委,FormMethod.Post,新{ID =frmStandingAddNAME =frmStandingAdd}))

但我不能让任何张贴到根即的http://域名/ API /人大常委会,而不是两个岗位的区​​域即 HTTP://域名/ AREANAME / API /人大常委会。我如何得到它正确地张贴?

更新:下面是我对相关领域路线:

 公众覆盖字符串AREANAME
{
    得到
    {
        返回AREANAME
    }
}公共覆盖无效RegisterArea(AreaRegistrationContext上下文)
{
    字符串defaultLocale =EN-US;    context.MapRoute(
        Areaname_default
        {语言code} / AREANAME / {控制器} / {行动} / {ID}
        新的语言{code = defaultLocale,控制器=主,行动=索引,ID = UrlParameter.Optional});    context.MapRoute(
        历史,
        {语言code} / AREANAME / {控制器} / {行动} / {}年,
        新{控制器=历史,行动=季节,一年= UrlParameter.Optional});
}

和我的Web API路线:

  config.Routes.MapHttpRoute(
    DefaultApi
    API / {}控制器/(编号),
    新{ID = RouteParameter.Optional}
);config.Routes.MapHttpRoute(
    DefaultApiWithAction
    API / {控制器} / {行动} / {}的季节,
    新{ID = RouteParameter.Optional}
);


解决方案

您可以明确地告诉环节由包括前导斜线张贴到根:

  @using(Ajax.BeginForm(,,空,新AjaxOptions {列举HTTPMethod =POST,URL =/ API /人大常委会},{新ID = frmStandingAddNAME =frmStandingAdd}))@using(Html.BeginForm(,/ API /常委,FormMethod.Post,新{ID =frmStandingAddNAME =frmStandingAdd}))

I am trying to get my page to post to my Web API controller, rather than my Area/Controller/Action. Here is what I have so far, I have tried using both Html.BeginForm and 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" }))

But I cannot get either to post to the root ie http://domain/api/Standing, instead both post to the Area ie http://domain/Areaname/api/Standing. How do I get it to post correctly?

Update: Here are my routes for the relevant area :

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 });
}

And my Web API routes :

config.Routes.MapHttpRoute(
    "DefaultApi",
    "api/{controller}/{id}",
    new { id = RouteParameter.Optional }
);

config.Routes.MapHttpRoute(
    "DefaultApiWithAction",
    "api/{controller}/{action}/{season}",
    new { id = RouteParameter.Optional }
);

解决方案

You can explicitly tell the links to post to the root by including the leading slash:

@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" }))

这篇关于Html.BeginForm路由到网页API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆