2

我正在使用http://attributerouting.net/ nuget包的WebApi。這裏是我的兩個GET方法和路徑屬性,列表和一個特定的項目:屬性路由約束路由

[GET("api/products/{tenantid}/{channelid}?{skip=0}&{take=20}&{status=1}")] 
public IEnumerable<Product> Get(short tenantId, byte channelId, int status, int skip, int take) 

[GET("api/products/{tenantid}/{channelid}/{id}")] 
public Story Get(short tenantId, byte channelId, long id) 

但在生成的幫助的URI,示出了三個GET選項。

GET api/products/{tenantid}/{channelid}?status={status}&skip={skip}&take={take} 
GET api/products/{tenantid}/{channelid}?id={id} 
GET api/products/{tenantid}/{channelid}/{id} 

即使「id」不是第一個GET方法的參數。如何在最後使用「?id = {id}」消除中間URI?我想我需要某種約束,但我無法從文檔網站中找到它。

回答

2
  1. 要解決此問題,您可以按不同方式命名操作。示例:GetAllProducts,GetProduct

  2. 您看到的問題是預期的行爲,因爲ApiExplorer(其中HelpPage使用)訪問路由集合內的所有路由,並檢查每條路由以查看可以從該路由到達哪些操作。現在使用上述屬性裝飾路線,路線集合中的路線最可能如下:

a。 「api/products/{tenantid}/{channelid}」,controller =「Products」,action =「Get」等...

b。 「api/products/{tenantid}/{channelid}/{id}」,controller =「Products」,action =「Get」...

現在對於a。路徑,ApiExplorer會檢查哪些操作可以到達並且它注意到對於控制器'產品'和'獲取'動作,有2個可以達到的動作,並且它試圖看到有多少參數來自路徑路徑本身,並且如果行動不是來自路徑路徑,它假定它來自查詢字符串...因此你看到「?id = {id}」。希望這可以幫助。

+0

修復它。我甚至沒有考慮改變方法名稱。我應該認識到,切換到備用路由技術時,方法名稱不是路由約定的一部分。 – Dzejms