我有一个Web API方法,需要两个双重参数:
I have a Web API method that takes two double args:
资源库界面:
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); }当我尝试调用此方法时,通过:
When I try to invoke this method, via:
localhost:28642/api/inventoryitems/GetDeptRange/1.1/99.99...我得到, HTTP错误404.0 - 未找到您要查找的资源已被删除,名称已更改,或暂时无法使用。
...I get, "HTTP Error 404.0 - Not Found The resource you are looking for has been removed, had its name changed, or is temporarily unavailable."
相关方法运行正常(本控制器上的其他方法)。
The related methods run fine (other methods on this Controller).
推荐答案我可以在我的机器上重现这个。
I was able to reproduce this on my machine.
只需在网址的末尾添加一个/我。看起来路由引擎正在以.99文件扩展名而不是输入参数来查看它。
Simply adding a / to the end of the URL corrected it for me. Looks like the routing engine is viewing it as a .99 file extension, rather than an input parameter.
localhost:28642/api/inventoryitems/GetDeptRange/1.1/99.99/另外,看起来你可以注册在使用内置助手生成链接时,自定义路径会自动将尾随斜线添加到URL的末尾。我没有亲自测试过: stackoverflow在每个URL的末尾添加尾部斜杠
Additionally, it looks like you can register a custom route that automatically adds a trailing slash to the end of the URL when using the built-in helpers to generate the link. I have not tested this personally: stackoverflow Add a trailing slash at the end of each url
最简单的解决方案是将以下行添加到RouteCollection中。不确定如何使用属性,但是在您的RouteConfig中,您只需添加以下内容:
The easiest solution is to just add the following line to your RouteCollection. Not sure how you would do it with the attribute, but in your RouteConfig, you just add this:
routes.AppendTrailingSlash = true;更多推荐
为什么我的Web API方法与双引号没有被调用?
发布评论