1

我正在使用Web API 2,它似乎已经调用了我现有的API调用,除非它复制了每个区域的所有调用。例如,说我有3个区域,并在其中的一个我有一个API调用,看起来像:Web API帮助页面复制所有区域的操作

public IList<string> GetStringList(string id) 
    { 
     //do work here... 
     return new List<string>{"a","b","c"}; 
    } 

如果我有3个区,那么Web API帮助页面会显示:

GET AREA1/API/MyAPIController/GetStringList/{ID}

GET AREA2/API/MyAPIController/GetStringList/{ID}

GET AREA3/API/MyAPIController/GetStringList/{ID}

和MyAPIController只存在于'area2'中。为什么会显示3次,我该如何解决?如果有帮助,我对区2区登记为:

public class Area2AreaRegistration : AreaRegistration 
{ 
    public override string AreaName 
    { 
     get 
     { 
      return "Area2"; 
     } 
    } 

    public override void RegisterArea(AreaRegistrationContext context) 
    { 
     context.MapRoute(
      "Area2_default", 
      "Area2/{controller}/{action}/{id}", 
      new { action = "Index", id = UrlParameter.Optional } 
     ); 

     context.Routes.MapHttpRoute(
    name: "Area2_ActionApi", 
    routeTemplate: "Area2/api/{controller}/{action}/{id}", 
    defaults: new { id = RouteParameter.Optional } 
); 

    } 
} 
+0

想到这个问题可能有一些做http://devillers.nl/getting-webapi-and-areas-to-play-nicely /,但这并没有最终导致修复问题 – Phil 2015-05-01 14:20:35

+0

我看到了同样的问题......你能弄明白吗? – Dave 2015-05-11 21:59:17

+0

不,还没有答案。希望能尽快拿出一些东西 – Phil 2015-05-11 22:45:50

回答

1

虽然不是一个解决问题的方法,你可以使用属性来路由映射的行动作为临时解决方法。

要启用路由的属性,请添加config.MapHttpAttributeRoutes();到WebApiConfig中的注册,该注册应位于App_Start文件夹内。

public static void Register(HttpConfiguration config) 
{ 
    // Attribute routing. 
    config.MapHttpAttributeRoutes(); 

    // Convention-based routing. 
    config.Routes.MapHttpRoute(
     name: "DefaultApi", 
     routeTemplate: "api/{controller}/{id}", 
     defaults: new { id = RouteParameter.Optional } 
    ); 
} 

一旦启用了路由的属性,你可以在一个动作指定路线:

public class BooksController : ApiController 
{ 
    [Route("api/books")] 
    public IEnumerable<Book> GetBooks() { ... } 
} 

可以read more here.看看路由前缀(如上图所示),并确保您启用路由属性如页面开头所示。

编辑:

你的情况:

[Route("area2/api/MyAPIController/GetStringList/{id}")] 
public IList<string> GetStringList(string id) 
{ 
    //do work here... 
    return new List<string>{"a","b","c"}; 
} 
+0

对不起,但我真的不知道你为什么这么说。它不会减少以上重复呼叫的数量,这是我唯一的问题。我不确定你的建议应该帮助解决什么问题。此外,我不属于属性路由的巨大粉丝,不管:https://maxtoroq.github.io/2014/02/why-aspnet-mvc-routing-sucks.html – Phil 2015-06-17 14:59:06

+0

我不喜欢属性路由,但它确实删除了我从多个地区收到的重复电话。我不认为我的答案是解决您的问题,只是解决方法。 – Speerian 2015-06-17 15:53:42