2013-05-12 10 views
11

Ich versuche, meine Seite auf meinem Web-API-Controller zu veröffentlichen, anstatt meine Area/Controller/Action. Hier ist, was ich bisher habe ich versucht, mit beiden Html.BeginForm und Ajax.Begin Form:Html.BeginForm Routing zu 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" })) 

Aber ich kann nicht entweder an die Wurzel dh zu schreiben http://domain/api/Standing, anstatt beide Post an die Umgebung dh http://domain/Areaname/api/Standing . Wie bekomme ich es richtig zu posten?

Update: Hier sind meine Wege für den relevanten Bereich:

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

Und meine Web-API-Routen:

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

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

Bitte posten Sie Ihr 'routes' – haim770

Antwort

9

Sie können explizit die Links sagen, an die Wurzel zu schreiben, indem der führende Schrägstrich:

@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

Danke, das Ajax.BeginForm funktioniert, aber die Html.BeginForm nicht, also ging ich mit Ajax.BeginForm – user517406

+0

hatte den ersten/aus "/ api/Standing"entfernen in Html.BeginForm. Mvc scheint standardmäßig einen hinzuzufügen, sonst würde ich // api/standing – Karsten

7

Sie müssten verwenden BeginRouteForm als Link-Generierung zu Web-API-Routen hängt immer vom Routennamen ab. Stellen Sie außerdem sicher, dass Sie den Routenwert httproute wie unten angegeben angeben.

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

bekommen. Das hat mir wirklich geholfen. Suchte das lange. –

Verwandte Themen