2013-05-12 2 views
11

Я пытаюсь получить мою страницу, чтобы опубликовать ее на моем веб-API-контроллере, а не на моем Area/Controller/Action. Вот то, что я до сих пор, я попытался использовать как Html.BeginForm и Ajax.Begin Форма:Html.BeginForm routing to Web Api

@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/Standing , Как мне получить сообщение правильно?

Update: Вот мои маршруты для соответствующей области:

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

Пожалуйста, оставьте свои 'routes' – haim770

ответ

9

Вы можете явно указать ссылки на сообщение с корнем, включая ведущая косая черта:

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

Спасибо, то Ajax.BeginForm работает, но Html.BeginForm не делает, поэтому я пошел с Ajax.BeginForm – user517406

+0

Пришлось удалить первый/с "/ API/Standing" в Html.BeginForm. Mvc, кажется, добавляет по умолчанию, так что в противном случае я бы получил // api/standing – Karsten

7

Вам необходимо будет использовать BeginRouteForm, поскольку создание ссылок на маршруты веб-API всегда зависит от имени маршрута. Также не забудьте указать значение маршрута под названием httproute, как показано ниже.

@using (Html.BeginRouteForm("DefaultApi", new { controller="Entries", httproute="true" })) 
+0

Это на самом деле помогло мне. Долго искал это. –