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