2012-11-30 70 views
3

瞭解了很多關於MapRouting的知識,並花費太多時間試圖獲得我需要做的事情,所以我希望有人能幫忙。 :)WebAPI路由協助

我希望做到以下幾點:

/API /實體/ 1 < - 視圖的細節實體的1 (這是一個字符串,而不是INT)

ID/api/Entities/1/Action < - 對ID爲1的實體調用特定操作。

/api /實體< - 查看實體設置。

/api /實體/操作< - 調用實體集上的特定操作。

我遇到的問題是最後一個。目前正在被第一個案件攔截,因爲id是一個字符串。

任何援助將不勝感激!

回答

2

一個很好的參考是here

不使用Attribute based routing答案有些長篇大論的RPC樣式(行動)和其餘部分的方法不共存很好,如果你匹配到同一個動詞。您已經注意到GET()和DOSOMETHING()在同一個控制器上被看作是重複的方法簽名。爲了解決這個問題,你可以嘗試使用兩個控制器:

所以我會建議使用attribute based routing;但是,它是可能的正常方法:

使用標準的路由...

設置像這樣的路線

 config.Routes.MapHttpRoute(
       name: "DefaultApi", 
       routeTemplate: "api/{controller}/{id}", 
       defaults: new { id = RouteParameter.Optional } 
      ); 
     config.Routes.MapHttpRoute(
      name: "ActionDefaultApi", 
      routeTemplate: "api/entities/{action}/{actionId}", 
      defaults: new 
          { 
           //Add more as needed 
           action = "(|dosomething|dosomethingelse)", 
           actionId = RouteParameter.Optional, 
           controller = "EntitiesRpc" 
          }); 

控制器1:

public class EntitiesController : ApiController 
{ 
    public string Get() 
    { 
     return "http://server/api/Entities"; 
    } 

    public string Get(string id) 
    { 
     return string.Format("http://server/api/Entities/{0}", id); 
    } 
} 

Con troller 2:

public class EntitiesRpcController : ApiController 
{ 
    [HttpGet] 
    public string DoSomething() 
    { 
     return "http://server/api/Entities/doSomething"; 
    } 

    [HttpGet] 
    public string DoSomething(string actionId) 
    { 
     return string.Format("http://server/api/Entities/doSomething/{0}", actionId); 
    } 

    [HttpGet] 
    public string DoSomethingElse() 
    { 
     return "http://server/api/Entities/doSomethingelse"; 
    } 

    [HttpGet] 
    public string DoSomethingElse(string actionId) 
    { 
     return string.Format("http://server/api/Entities/doSomethingelse/{0}", actionId); 
    } 
} 

現在assumming您使用基於屬性的路由你可以回到一個控制器,並使用這樣的事:

public class EntitiesController : ApiController 
{ 
    [Get("Entities")] 
    public string Get() 
    { 
     return "http://server/api/Entities"; 
    } 

    [Get("Entities/{id}")] 
    public string Get(string id) 
    { 
     return string.Format("http://server/api/Entities/{0}", id); 
    } 

    [Get("Entities/doSomething")] 
    public string DoSomething() 
    { 
     return "http://server/api/Entities/doSomething"; 
    } 

    [Get("Entities/doSomething/{actionId}")] 
    public string DoSomething(string actionId) 
    { 
     return string.Format("http://server/api/Entities/doSomething/{0}", actionId); 
    } 

    [Get("Entities/doSomethingelse")] 
    public string DoSomethingElse() 
    { 
     return "http://server/api/Entities/doSomethingelse"; 
    } 

    [Get("Entities/doSomethingelse/{actionId}")] 
    public string DoSomethingElse(string actionId) 
    { 
     return string.Format("http://server/api/Entities/doSomethingelse/{0}", actionId); 
    } 
} 
+1

AttributeRouting.net確實的答案。非常非常酷。 :) – MichaelDBang

+0

@MichaelDBang很高興它幫助。如果您對此感到滿意,請您也接受答案? –

+0

啊......這樣一個新手。我試圖「起來」它,但它不允許我......沒有看到複選框。所以你去!再次感謝。 – MichaelDBang