2014-01-27 122 views
6

我有一個帶有兩個雙ARGS一個Web API方法:爲什麼我的Web API方法沒有調用雙參數?

庫接口:

public interface IInventoryItemRepository 
{ 
. . . 
    IEnumerable<InventoryItem> GetDepartmentRange(double deptBegin, double deptEnd); 
. . . 
} 

庫:

public IEnumerable<InventoryItem> GetDepartmentRange(double deptBegin, double deptEnd) 
{ 
    // Break the doubles into their component parts: 
    int deptStartWhole = (int)Math.Truncate(deptBegin); 
    int startFraction = (int)((deptBegin - deptStartWhole) * 100); 
    int deptEndWhole = (int)Math.Truncate(deptEnd); 
    int endFraction = (int)((deptBegin - deptEndWhole) * 100); 

    return inventoryItems.Where(d => d.dept >= deptStartWhole).Where(e => e.subdept >= startFraction) 
     .Where(f => f.dept <= deptEndWhole).Where(g => g.subdept >= endFraction); 
} 

控制器:

[Route("api/InventoryItems/GetDeptRange/{BeginDept:double}/{EndDept:double}")] 
public IEnumerable<InventoryItem> GetInventoryByDeptRange(double BeginDept, double EndDept) 
{ 
    return _inventoryItemRepository.GetDepartmentRange(BeginDept, EndDept); 
} 

當我嘗試調用此方法,通過:

http://localhost:28642/api/inventoryitems/GetDeptRange/1.1/99.99 

...我得到的,「HTTP錯誤404.0 - 找不到 您正在尋找已被刪除的資源,有其名稱更改,或者暫時不可用。

的相關方法運行正常(這個控制器的其他方法)。

回答

14

我能夠重現這在我的機器上。

簡單地增加一個/到URL的末尾糾正它我,貌似路由引擎瀏覽時爲0.99文件擴展名,而不是輸入參數。

http://localhost:28642/api/inventoryitems/GetDeptRange/1.1/99.99/ 

此外,它看起來像你可以註冊自動添加一個尾部斜槓結束自定義路線的使用內置助手生成鏈接時的URL。我沒有親自測試: stackoverflow Add a trailing slash at the end of each url

最簡單的解決方案是將下面的行添加到RouteCollection中。不知道你將如何與屬性做到這一點,但在你的RouteConfig,你只補充一點:

routes.AppendTrailingSlash = true; 
+0

鬆了幾個小時後很高興找到答案! –

6

正如喬爾指出,這可能是IIS文件擴展名拾起,並試圖成爲一個靜態文件。你可以解決這個問題通過添加以下到您的web.config文件(system.webServer下):

<modules> 
    <remove name="UrlRoutingModule-4.0" /> 
    <add name="UrlRoutingModule-4.0" type="System.Web.Routing.UrlRoutingModule" 
       preCondition="" /> 
</modules> 

默認情況下,IIS將只運行該模塊它所認爲是對ASP.NET資源的請求 - 在上面在每個站點基礎上刪除這個條件,允許您通過ASP.NET MVC/Web API路由路由所有請求。

如果靜態文件存在,它仍然是首選,所以這不應該引起其他問題。

相關問題